向 MongoDB 集合中的每个文档添加新字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7714216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add new field to every document in a MongoDB collection
提问by itsme
How can I add a new field to every document in an existent collection?
如何向现有集合中的每个文档添加新字段?
I know how to update an existing document's field but not how to add a new field to every document in a collection. How can I do this in the mongo
shell?
我知道如何更新现有文档的字段,但不知道如何向集合中的每个文档添加新字段。我怎样才能在mongo
shell 中做到这一点?
回答by RameshVel
Same as the updating existing collection field, $set
will add a new fields if the specified field does not exist.
与更新现有集合字段相同,$set
如果指定字段不存在,将添加新字段。
Check out this example:
看看这个例子:
> db.foo.find()
> db.foo.insert({"test":"a"})
> db.foo.find()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
> item = db.foo.findOne()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
> db.foo.update({"_id" :ObjectId("4e93037bbf6f1dd3a0a9541a") },{$set : {"new_field":1}})
> db.foo.find()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "new_field" : 1, "test" : "a" }
EDIT:
编辑:
In case you want to add a new_field to all your collection, you have to use empty selector, and set multi flag to true (last param) to update all the documents
如果要向所有集合添加 new_field,则必须使用空选择器,并将 multi 标志设置为 true(最后一个参数)以更新所有文档
db.your_collection.update(
{},
{ $set: {"new_field": 1} },
false,
true
)
EDIT:
编辑:
In the above example last 2 fields false, true
specifies the upsert
and multi
flags.
在上面的例子中,最后 2 个字段false, true
指定了upsert
和multi
标志。
Upsert:If set to true, creates a new document when no document matches the query criteria.
Upsert:如果设置为 true,则在没有文档与查询条件匹配时创建一个新文档。
Multi:If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.
Multi:如果设置为 true,则更新满足查询条件的多个文档。如果设置为 false,则更新一个文档。
This is for Mongo versions
prior to 2.2
. For latest versions the query is changed a bit
这是针对 Mongoversions
之前的2.2
。对于最新版本,查询略有变化
db.your_collection.update({},
{$set : {"new_field":1}},
{upsert:false,
multi:true})
回答by LineDrop
To clarify, the syntax is as follows for MongoDB version 4.0.x:
澄清一下,MongoDB 4.0.x 版的语法如下:
db.collection.update({},{$set: {"new_field*":1}},false,true)
Here is a working example adding a publishedfield to the articlescollection and setting the field's value to true:
这是一个向文章集合添加已发布字段并将该字段的值设置为true的工作示例:
db.articles.update({},{$set: {"published":true}},false,true)
回答by Alex Jolig
Pymongo 3.9+
派蒙戈 3.9+
update()
is now deprecatedand you should use replace_one()
, update_one()
, or update_many()
instead.
update()
现在已弃用,您应该使用replace_one()
, update_one()
, 或update_many()
代替。
In my case I used update_many()
and it solved my issue:
在我的情况下,我使用update_many()
并解决了我的问题:
db.your_collection.update_many({}, {"$set": {"new_field": "value"}}, upsert=False, array_filters=None)
From documents
从文件
update_many(filter, update, upsert=False, array_filters=None, bypass_document_validation=False, collation=None, session=None) filter: A query that matches the documents to update. update: The modifications to apply. upsert (optional): If True, perform an insert if no documents match the filter. bypass_document_validation (optional): If True, allows the write to opt-out of document level validation. Default is False. collation (optional): An instance of Collation. This option is only supported on MongoDB 3.4 and above. array_filters (optional): A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+. session (optional): a ClientSession.
update_many(filter, update, upsert=False, array_filters=None, bypass_document_validation=False, collation=None, session=None) filter: A query that matches the documents to update. update: The modifications to apply. upsert (optional): If True, perform an insert if no documents match the filter. bypass_document_validation (optional): If True, allows the write to opt-out of document level validation. Default is False. collation (optional): An instance of Collation. This option is only supported on MongoDB 3.4 and above. array_filters (optional): A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+. session (optional): a ClientSession.