Home > Net >  updateMany mongoose with multiple condition
updateMany mongoose with multiple condition

Time:10-22

I have two documents as in db bookingDetails" as:

{
    "_id":ObjectId("12jj21jan21jdo3kan2idm11"),
    "book_status":"BOOKED",
    "reservation":false,
}

{
    "_id":ObjectId("12jj21jan21jdo3kan2idm11"),
    "book_status":"ACCEPTED",
    "reservation":true,
}

update both of the document with multiple queries.

I am trying as,

bookingDetails.updateMany(
    {
        "book_status":"ACCEPTED",
    },
    {
         $set:{
             "book_status":"DONE"
         }
    }
)

bookingDetails.updateMany(
    {
        "book_status":"BOOKED",
    },
    {
         $set:{
             "book_status":"DONE"
         }
    }
)

But this method is repetitive which increases load on db. Is there any way that it can be optimized?

Please let me know if anyone needs any further information.

CodePudding user response:

You can use an $or like this:

bookingDetails.updateMany({
  "$or": [
    {
      "book_status": "ACCEPTED"
    },
    {
      "book_status": "BOOKED"
    }
  ]
},
{
  "$set": {
    "book_status": "DONE"
  }
})

Example here

  • Related