简体   繁体   English

updateMany 具有多种条件的猫鼬

[英]updateMany mongoose with multiple condition

I have two documents as in db bookingDetails" as:我有两个文件,如 db bookingDetails" 为:

{
    "_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:你可以像这样使用$or

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

Example here示例在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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