在 MongoDB shell 中更新查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7756661/
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
Update query in MongoDB shell
提问by Shamoon
In the shell, my query is:
在 shell 中,我的查询是:
db.checkin_4e95ae0926abe9ad28000001.update({location_city:"New York"}, {location_country: "FUDGE!"});
However, it doesn't actually update my records. It doesn't error either. When I do a db.checkin_4e95ae0926abe9ad28000001.find({location_city:"New York"});
after running this, I get all my results but the location_country
has not changed:
但是,它实际上并没有更新我的记录。它也不会出错。当我db.checkin_4e95ae0926abe9ad28000001.find({location_city:"New York"});
在运行后执行此操作时,我得到了所有结果,但location_country
没有改变:
{
"_id": ObjectId("4e970209a0290b70660009e9"),
"addedOn": ISODate("2011-10-13T15:21:45.772Z"),
"location_address1": "",
"location_city": "New York",
"location_country": "United States",
"location_latLong": {
"xLon": -74.007124,
"yLat": 40.71455
},
"location_source": "socialprofile",
"location_state": "New York",
"location_zip": ""
}
采纳答案by Amitesh
Changed in version 3.6. Following is the syntax for update :
在 3.6 版中更改。以下是更新的语法:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
Example :
例子 :
db.getCollection('products').update({},{$unset: {translate:1, qordoba_translation_version:1}}, {multi: true})
In your example :
在你的例子中:
db.checkin_4e95ae0926abe9ad28000001.update(
{location_city:"New York"}, //query
// $update query
{ $set : { location_country: "FUDGE!"}});
By default, the update() method updates a single document. Set the Multi Parameter to update all documents that match the query criteria.
默认情况下, update() 方法更新单个文档。设置多参数以更新与查询条件匹配的所有文档。
Example 2 :
示例 2:
db.checkin_4e95ae0926abe9ad28000001.update(
{location_city:"New York"}, //query
// $update query
{ $set : { location_country: "FUDGE!"}}, {multi: true});
回答by Andrew Orsich
This is because in second parameter of updatefunction you need to use $setoperator to update location_country
as in example below:
这是因为在更新函数的第二个参数中,您需要使用$set运算符来更新location_country
,如下例所示:
db.checkin_4e95ae0926abe9ad28000001.update(
{location_city:"New York"}, //find criteria
// this row contains fix with $set oper
{ $set : { location_country: "FUDGE!"}});
Hereyou can find a list of available update operators.
您可以在此处找到可用更新运算符的列表。
回答by Sagar Jadhav
db.m_country.update(
{"countryId": "962a0935-bf3d-4f63-a53c-254760273ede"},
{$set: {'countryPopulation': '12540000'}})