Home > Enterprise >  Aggregate function not adding new column MongoDB
Aggregate function not adding new column MongoDB

Time:11-18

When I execute below column it is giving results as column is added , but when I query the table, new column is not showing up.

db.getCollection("arcm_qc").aggregate([
 {
        $addFields: {
            MGE_ID: { $arrayElemAt: [{ "$split": ["$MCA Go", "-"] }, 0] }

        }
    }

])

CodePudding user response:

You can't add fields in the collection using only aggregate. But you can use aggregate in an update query like this:

db.collection.update({/*the documents you want to update or empty to update all*/},
[
  {
    $addFields: {
      MGE_ID: {
        $arrayElemAt: [{"$split": ["$MCA Go","-"]},0]
      }
    }
  }
],
{
  multi: true
})

Example here

  • Related