Home > Back-end >  How to use update aggregation in MongoDb
How to use update aggregation in MongoDb

Time:11-11

my sample db:

"Employee": [ { "empeId": "e001",
             "fName": "James",
             "lName": "Bond",
             "email": "[email protected]",
             "experience": [
                    "Database Design",
                    "SQL",
                    "Java" ]
                },
                { "empeId": "e002",
              "fName": "Harry",
              "lName": "Potter",
              "experience": [
                    "Data Warehouse",
                    "SQL",
                    "Spark Scala",
                    "Java Scripts" ]
                } ],
  "Project": [ { "projectId": "p001",
            "projectTitle": "Install MongoDB" },
                {   "projectId": "p002",
            "projectTitle": "Install Oracle" },
                {   "projectId": "p003",
            "projectTitle": "Install Hadoop" } ],
  "EmployeeProject": [ {  "empeId": "e001",
                   "projectId": "p001",
                   "hoursWorked": 4 },
                     { "empeId": "e001",
                   "projectId": "p003",
                   "hoursWorked": 2 },
                     { "empeId": "e002",
                   "projectId": "p003",
                   "hoursWorked": 5 } ]

I want to update the array experience with 'test' of user with empeId: e001

desired output:

"Employee": [ { "empeId": "e001",
             "fName": "James",
             "lName": "Bond",
             "email": "[email protected]",
             "experience": [
                    "Database Design",
                    "SQL",
                    "Java",
                    "test"
]
                }]

I tried using

db.emp.updateOne([
   {$unwind: "$Employee"},
   {$match: {"Employee.emepId" : "e001" }}
], 
{$push: {"Employee.experience" : "test"}})

and I get Syntax Error: Invalid property id @(shell):1:31

is this how the syntax for update with pipeline aggregation works?

CodePudding user response:

The easiest and clean way(using arrayFilters):

db.collection.update({ Employee.empeId" : "e001"},
{
  "$push": {
   "Employee.$[x].experience": "test"
  }
 },
 {
  arrayFilters: [
  {
   "x.empeId": "e001"
  }
 ]
})

Playground1

And via update/aggregation:

 db.collection.update({ Employee.empeId" : "e001" },
 [
 {
  $set: {
  "Employee": {
    $map: {
      input: "$Employee",
      as: "m",
      in: {
        $cond: [
          {
            $eq: [
              "$$m.empeId",
              "e001"
            ]
          },
          {
            $mergeObjects: [
              "$$m",
              {
                experience: {
                  $concatArrays: [
                    "$$m.experience",
                    [
                      "test"
                    ]
                  ]
                }
              }
            ]
          },
          "$$m"
        ]
      }
    }
    }
   }
  }
 ])

Playground2

CodePudding user response:

Solved this with:

db.empeProject.update{{"Employee.empeId" : "e001" },{$push: {"Employee.$.experience","test"}})
  • Related