user15780176
user15780176

Reputation: 129

Aggregate function not adding new column MongoDB

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] }

        }
    }

])

Upvotes: 0

Views: 213

Answers (1)

J.F.
J.F.

Reputation: 15215

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

Upvotes: 1

Related Questions