updateMany mongoose with multiple condition

Viewed 175

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.

1 Answers

You can use an $or like this:

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

Example here

Related