Sha*_*oon 21 mongodb mongo-shell
在shell中,我的查询是:
db.checkin_4e95ae0926abe9ad28000001.update({location_city:"New York"}, {location_country: "FUDGE!"});
Run Code Online (Sandbox Code Playgroud)
但是,它实际上并没有更新我的记录.它也没有错误.当我执行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": ""
}
Run Code Online (Sandbox Code Playgroud)
And*_*ich 29
这是因为在更新函数的第二个参数中,您需要使用$ set运算符进行更新location_country,如下例所示:
db.checkin_4e95ae0926abe9ad28000001.update(
{location_city:"New York"}, //find criteria
// this row contains fix with $set oper
{ $set : { location_country: "FUDGE!"}});
Run Code Online (Sandbox Code Playgroud)
在这里,您可以找到可用更新运算符的列表.
小智 8
db.m_country.update(
{"countryId": "962a0935-bf3d-4f63-a53c-254760273ede"},
{$set: {'countryPopulation': '12540000'}})
Run Code Online (Sandbox Code Playgroud)
版本3.6中已更改.以下是更新的语法:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
Run Code Online (Sandbox Code Playgroud)
示例:
db.getCollection('products').update({},{$unset: {translate:1, qordoba_translation_version:1}}, {multi: true})
Run Code Online (Sandbox Code Playgroud)
在你的例子中:
db.checkin_4e95ae0926abe9ad28000001.update(
{location_city:"New York"}, //query
// $update query
{ $set : { location_country: "FUDGE!"}});
Run Code Online (Sandbox Code Playgroud)
默认情况下,update()方法更新单个文档.设置多参数以更新符合查询条件的所有文档.
例2:
db.checkin_4e95ae0926abe9ad28000001.update(
{location_city:"New York"}, //query
// $update query
{ $set : { location_country: "FUDGE!"}}, {multi: true});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46462 次 |
| 最近记录: |