1

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": "jamesbond@hotmail.com",
             "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

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
Bryead
  • 120
  • 6

2 Answers2

1

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"
    }
  ]
})
Yong Shun
  • 35,286
  • 4
  • 24
  • 46
1

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"}}
)
Kishan Lal
  • 473
  • 1
  • 5
  • 13