问题
How can I add few elements to an array in every document in a collection?
I know how to update documents, but I don't know how to update every document in a collection.
For example, I have a document like this one:
Document
{
"user": 123456789,
"packs": {
"normal": 0,
"rare": 0,
}
}
Let's say that I updated my bot and now every player should also have a "super-rare": 0
element in his document. How can I achieve this? I'm using MongoDB Atlas
回答1:
You can get this done with simple update. If you want to update all documents, specify empty condition {} and multi: true like below
db.usertest.update( {}, {$set: {"packs.super-rare": 0}}, false, true)
Empty {} - To match all documents false - upsert true - multi (update multiple documents)
来源:https://stackoverflow.com/questions/57708184/mongodb-updating-every-document-in-a-collection