[英]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 }
)
I was thinking: 我刚在想:
db.people.update(
[{ "age": "Thirty Two" }, { age: 32 }, { upsert: false }],
[{ "age": "Fifty-Five" }, { age: 55 }, { upsert: false }],
[{ "age": "Twenty" }, { age: 20 }, { upsert: false }]
)
But that didn't work.. I know im just really tired and can't think of it... 但这没用..我知道我真的很累,无法想到...
Perhaps this is what you want. 也许这就是您想要的。
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 }]
]);
To elaborate on my comment and present an alternative to db.eval()
, a "operatorless" update replaces the entire matched document with the update document (except keeping the _id
): 为了详细说明我的意见并提出
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 }
Changing only one field means using an update operator like $set
: 仅更改一个字段意味着使用
$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 }
By default, updates affect only one matching document. 默认情况下,更新仅影响一个匹配的文档。 The option
multi=true
will cause the update to affect all matching documents: 选项
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 }
So, to do the updates you asked about in your question in the shell, I'd use a for loop but dispense with the db.eval
in favor of multiple calls to db.people.update()
因此,要在外壳程序中执行您在问题中询问的更新,我将使用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])
})
If you have hundreds of updates and are using MongoDB 2.6 or later, look into bulk operations . 如果您有数百个更新,并且正在使用MongoDB 2.6或更高版本,请查看批量操作 。 There's more information in the
db.collection.update()
docs . db.collection.update()
文档中有更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.