MongoDB: Updating every document in a collection

风格不统一 提交于 2021-01-29 17:57:41

问题


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

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