Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i add a new item to array in mongodb

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

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

and

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading