Mongodb: upsert multiple documents at the same time (with key like $in)

落花浮王杯 提交于 2021-01-29 18:38:25

问题


I stumbled upon a funny behavior in MongoDB:

When I run:

db.getCollection("words").update({ word: { $in: ["nico11"] } }, { $inc: { nbHits: 1 } }, { multi: 1, upsert: 1 })

it will create "nico11" if it doesn't exist, and increase nbHits by 1 (as expected).

However, when I run:

db.getCollection("words").update({ word: { $in: ["nico10", "nico11", "nico12"] } }, { $inc: { nbHits: 1 } }, { multi: 1, upsert: 1 })

it will correctly update the keys that are already in the DB, but not insert the missing ones.

Is that the expected behavior, and is there any way I can provide an array to mongoDB, for it to update the existing elements, and create the ones that need to be created?


回答1:


That is expected behaviour according to the documentation:

The update creates a base document from the equality clauses in the parameter, and then applies the update expressions from the parameter. Comparison operations from the will not be included in the new document.

And, no, there is no way to achieve what you are attempting to do here using a simple upsert. The reason for that is probably that the expected outcome would be impossible to define. In your specific case it might be possible to argue along the lines of: "oh well, it is kind of obvious what we should be doing here". But imagine a more complex query like this:

db.getCollection("words").update({
    a: { $in: ["b", "c" ] },
    x: { $in: [ "y", "z" ]}
},
{ $inc: { nbHits: 1 } },
{ multi: 1, upsert: 1 })

What should MongoDB do in this case?

There is, however, the concept of bulk write operations in MongoDB where you would need to define three separate updateOne operations and package them up in a single request to the server.



来源:https://stackoverflow.com/questions/52431213/mongodb-upsert-multiple-documents-at-the-same-time-with-key-like-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!