skip to Main Content

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
                    }
                  }
                }
          }}

2

Answers


  1. 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

    Login or Signup to reply.
  2. by specify paperid

    db.collection.updateOne(
        {},
        {
            $set: { "professor.department.$[].review.$[r].status": 'finish' }
        },
        {
            arrayFilters: [{ "r.paperid": '1' }]
        }
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search