Home > Enterprise >  MongoDB update inside array of object of array of object
MongoDB update inside array of object of array of object

Time:06-20

Test Data:-

professor:{
 name:"temp1",
 department:[
 {name:"pro1",
 review:[
 {paper:"paper1",status:"review",paperid:"1"}
 ]}]}

I want to update the status "review" to "finish". For to search I use :

 {
department: {
                $elemMatch: {
                  review: {
                    $elemMatch: {
                      paperid: id
                    }
                  }
                }
          }}

CodePudding user response:

If you want to update all your documents where the status is review use:

db.collection.update(
  {},
  {$set: {"professor.department.$[].review.$[r].status": "finish"}},
  {arrayFilters: [{"r.status": "review"}]}
)

See how it works on the playground example

If you want to update only where the department.name is pro1 use:

db.collection.update(
  {},
  {$set: {"professor.department.$[d].review.$[r].status": "finish"}},
  {arrayFilters: [{"d.name": "pro1"}, {"r.status": "review"}]}
)

See how it works on the playground example - department

CodePudding user response:

by specify paperid

db.collection.updateOne(
    {},
    {
        $set: { "professor.department.$[].review.$[r].status": 'finish' }
    },
    {
        arrayFilters: [{ "r.paperid": '1' }]
    }
)
  • Related