简体   繁体   English

基于ID更新MongoDB中的多个文档

[英]Updating Multiple Documents In MongoDB Based on ID

So I've seen this question asked before, but when I tried the solution myself it didn't seem to work.所以我之前看过这个问题,但是当我自己尝试解决方案时,它似乎没有用。 Update multiple documents by id set. 通过 id 集更新多个文档。 Mongoose 猫鼬

Based on the solution above, I wrote this code:基于上面的解决方案,我写了这段代码:

listOfParticipant = [ "5f50a1df63fe0000ed98b393", "5f69cc7cda1d549f9edcc8bb","5f3207204450b32620449657","5f3207204450b32620449657", "5f39585030027a3d57f0cc0e", "5e719e4715c7865bcb00c820", "5f32d0ab59eccf22d2e2f29f" ],
    
    User.update( {_id: {$in: listOfParticipant}},
              {$addToSet: {
                userNotification: notification
              }
            },
            {multi: true}
            )

But it doesn't seem to work, could anyone help me out in finding out why?但它似乎不起作用,有人能帮我找出原因吗?

So this query is correct.所以这个查询是正确的。 The reason it wasn't working is because I was trying to chain it with another MongoDB call.它不起作用的原因是因为我试图将它与另一个 MongoDB 调用链接起来。 I was trying to use it inside the callback function when I updated another MongoDB collection.当我更新另一个 MongoDB 集合时,我试图在回调函数中使用它。 Once I made this into its own separate function, it worked just fine!一旦我把它变成了它自己的独立函数,它就工作得很好!

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

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