Bryead
Bryead

Reputation: 120

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

Upvotes: 1

Views: 2962

Answers (2)

Kishan Lal
Kishan Lal

Reputation: 523

You are trying to update something that's inside an array. MongoDB uses the positional operator($) that identifies the correct element in the array to update without explicitly specifying the position of the element in the array.

Can you try the below one? (I haven't tried on my own but I believe it should work)

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

Upvotes: 1

Yong Shun
Yong Shun

Reputation: 51125

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

Upvotes: 1

Related Questions