[英]How could I run this as one command in Mongo Shell
db.people.update(
{ "age": "Thirty Two" }, { age: 32 }, { upsert: false }
)
db.people.update(
{ "age": "Fifty-Five" }, { age: 55 }, { upsert: false }
)
db.people.update(
{ "age": "Twenty" }, { age: 20 }, { upsert: false }
)
我刚在想:
db.people.update(
[{ "age": "Thirty Two" }, { age: 32 }, { upsert: false }],
[{ "age": "Fifty-Five" }, { age: 55 }, { upsert: false }],
[{ "age": "Twenty" }, { age: 20 }, { upsert: false }]
)
但这没用..我知道我真的很累,无法想到...
也许这就是您想要的。
db.eval(function(name, options) {
var coll = db.getCollection(name);
for (var x in options) {
var option = options[x];
coll.update(option[0], option[1], option[2]);
}
},
"people",
[
[{ "age": "Thirty Two" }, { age: 32 }, { upsert: false }],
[{ "age": "Fifty-Five" }, { age: 55 }, { upsert: false }],
[{ "age": "Twenty" }, { age: 20 }, { upsert: false }]
]);
为了详细说明我的意见并提出db.eval()
的替代方案,“无操作员”更新将整个匹配的文档替换为更新文档(保留_id
除外):
> db.people.find()
{ "_id" : 0, "name" : "Barney Big", "age" : 32 }
{ "_id" : 1, "name" : "Sam Small", "age" : 26 }
{ "_id" : 2, "name" : "Mindy Medium", "age" : 26 }
> db.people.update({ "age" : 32 }, { "age" : 45 })
> db.people.find()
{ "_id" : 0, "age" : 45 }
{ "_id" : 1, "name" : "Sam Small", "age" : 26 }
{ "_id" : 2, "name" : "Mindy Medium", "age" : 26 }
仅更改一个字段意味着使用$set
这样的更新运算符 :
> db.people.find()
{ "_id" : 0, "name" : "Barney Big", "age" : 32 }
{ "_id" : 1, "name" : "Sam Small", "age" : 26 }
{ "_id" : 2, "name" : "Mindy Medium", "age" : 26 }
> db.people.update({ "age" : 32 }, { "$set" : { "age" : 45 } })
> db.people.find()
{ "_id" : 0, "name" : "Barney Big", "age" : 45 }
{ "_id" : 1, "name" : "Sam Small", "age" : 26 }
{ "_id" : 2, "name" : "Mindy Medium", "age" : 26 }
默认情况下,更新仅影响一个匹配的文档。 选项multi=true
将导致更新影响所有匹配的文档:
> db.people.find()
{ "_id" : 0, "name" : "Barney Big", "age" : 32 }
{ "_id" : 1, "name" : "Larry Large", "age" : 32 }
{ "_id" : 2, "name" : "Sam Small", "age" : 26 }
{ "_id" : 3, "name" : "Mindy Medium", "age" : 26 }
> db.people.update({ "age" : 32 }, { "$set" : { "age" : 45 } })
> db.people.find()
{ "_id" : 0, "name" : "Barney Big", "age" : 45 }
{ "_id" : 1, "name" : "Larry Large", "age" : 32 }
{ "_id" : 2, "name" : "Sam Small", "age" : 26 }
{ "_id" : 3, "name" : "Mindy Medium", "age" : 26 }
> db.people.update({ "age" : 26 }, { "$set" : { "age" : 23 } }, { "multi" : true })
> db.people.find()
{ "_id" : 0, "name" : "Barney Big", "age" : 45 }
{ "_id" : 1, "name" : "Larry Large", "age" : 32 }
{ "_id" : 2, "name" : "Sam Small", "age" : 23 }
{ "_id" : 3, "name" : "Mindy Medium", "age" : 23 }
因此,要在外壳程序中执行您在问题中询问的更新,我将使用for循环,但省却了db.eval
,而多次调用db.people.update()
> updates = [
[{ "age": "Thirty Two" }, { "$set" : { "age" : 32 } }],
[{ "age": "Fifty-Five" }, { "$set" : { "age" : 55 } }],
[{ "age": "Twenty" }, { "$set" : { "age" : 20 } }]
]
> updates.forEach(function(pair) {
db.people.update(pair[0], pair[1])
})
如果您有数百个更新,并且正在使用MongoDB 2.6或更高版本,请查看批量操作 。 db.collection.update()
文档中有更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.