Search code examples
mongodbmongodb-querymongodb-update

MongoDB - How to add a new item into an array


How do I use the method 𝑢𝑝𝑑𝑎𝑡𝑒() to implement the following changes?

Add a new experience "HIVE" to the employee whose empeId is 'e001'.

and

Change the email account for employee e001 to "jamesbond$hotmail.com".

Below is the database in question

db.empeProject.insert([ {
  "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 } ]
} ] );

Currently what I've tried for the first is

db.empeProject.update(
  {"Employee.empeId":"e001"},
  {"$push":{"Employee.experience":"HIVE"}}
)

and the second is

db.empeProject.update(
  {"Employee.empeId":"e001"},{"$set": 
  {"Employee.email":"jamesbond$hotmail.com"}}
)

In both cases, I got an error

cannot create field in element


Solution

  • Solution 1

    You need a $ operator to update the first matched element in the array for both scenarios.

    db.empeProject.update({
      "Employee.empeId": "e001"
    },
    {
      "$push": {
        "Employee.$.experience": "HIVE"
      }
    })
    

    Demo Solution 1 for Q1 @ Mongo Playground

    db.empeProject.update({
      "Employee.empeId": "e001"
    },
    {
      "$set": {
        "Employee.$.email": "jamesbond$hotmail.com"
      }
    })
    

    Demo Solution 1 for Q2 @ Mongo Playground


    Solution 2

    Besides, you may also work with $[<identifier>] filtered positional operator and arrayFilters as well.

    db.empeProject.update({
      "Employee.empeId": "e001"
    },
    {
      "$push": {
        "Employee.$[emp].experience": "HIVE"
      }
    },
    {
      arrayFilters: [
        {
          "emp.empeId": "e001"
        }
      ]
    })
    
    db.empeProject.update({
      "Employee.empeId": "e001"
    },
    {
      "$set": {
        "Employee.$[emp].email": "jamesbond$hotmail.com"
      }
    },
    {
      arrayFilters: [
        {
          "emp.empeId": "e001"
        }
      ]
    })