MongoDB:更新集合中的每个文档 [英] MongoDB: Updating every document in a collection
问题描述
如何向集合中每个文档的数组添加少量元素?
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:
文档
{
"user": 123456789,
"packs": {
"normal": 0,
"rare": 0,
}
}
假设我更新了我的机器人,现在每个玩家的文档中也应该有一个 "super-rare": 0
元素.我怎样才能做到这一点?我正在使用 MongoDB Atlas
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
推荐答案
您可以通过简单的更新来完成这项工作.如果要更新所有文档,请指定空条件 {} 和 multi: true 如下
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 {} - 匹配所有文档错误的向上插入true - multi(更新多个文档)
Empty {} - To match all documents false - upsert true - multi (update multiple documents)
这篇关于MongoDB:更新集合中的每个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!