简体   繁体   中英

updateMany mongoose with multiple condition

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.

You can use an $or like this:

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

Example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM