在MongoDB中,是否可以使用来自另一个字段的值更新一个字段的值?等价的SQL是这样的:
UPDATE Person SET Name = FirstName + ' ' + LastName
MongoDB的伪代码是:
db.person.update( {}, { $set : { name : firstName + ' ' + lastName } );
在MongoDB中,是否可以使用来自另一个字段的值更新一个字段的值?等价的SQL是这样的:
UPDATE Person SET Name = FirstName + ' ' + LastName
MongoDB的伪代码是:
db.person.update( {}, { $set : { name : firstName + ' ' + lastName } );
当前回答
在MongoDB 4.2+版本中,更新更加灵活,因为它允许在更新、updateOne和updateMany中使用聚合管道。你现在可以使用聚合操作符转换你的文档,然后更新,而不需要显式地声明$set命令(而不是使用$replaceRoot: {newRoot: "$$ROOT"})
在这里,我们使用聚合查询从MongoDB的ObjectID“_id”字段提取时间戳,并更新文档(我不是SQL专家,但我认为SQL不提供任何自动生成的ObjectID,有时间戳,你必须自动创建该日期)
var collection = "person"
agg_query = [
{
"$addFields" : {
"_last_updated" : {
"$toDate" : "$_id"
}
}
},
{
$replaceRoot: {
newRoot: "$$ROOT"
}
}
]
db.getCollection(collection).updateMany({}, agg_query, {upsert: true})
其他回答
显然,自从MongoDB 3.4以来,有一种方法可以有效地做到这一点,请参阅styvane的答案。
过时的答案如下
您还不能在更新中引用文档本身。您需要遍历文档并使用函数更新每个文档。请参阅下面的示例,或者服务器端eval()的示例。
(我本想把这篇文章作为评论,但我做不到)
对于登陆这里试图使用c#驱动程序更新文档中的另一个字段的任何人… 我不知道如何使用任何UpdateXXX方法及其相关重载,因为它们将UpdateDefinition作为参数。
// we want to set Prop1 to Prop2
class Foo { public string Prop1 { get; set; } public string Prop2 { get; set;} }
void Test()
{
var update = new UpdateDefinitionBuilder<Foo>();
update.Set(x => x.Prop1, <new value; no way to get a hold of the object that I can find>)
}
作为一种变通方法,我发现可以在IMongoDatabase (https://docs.mongodb.com/manual/reference/command/update/#dbcmd.update)上使用RunCommand方法。
var command = new BsonDocument
{
{ "update", "CollectionToUpdate" },
{ "updates", new BsonArray
{
new BsonDocument
{
// Any filter; here the check is if Prop1 does not exist
{ "q", new BsonDocument{ ["Prop1"] = new BsonDocument("$exists", false) }},
// set it to the value of Prop2
{ "u", new BsonArray { new BsonDocument { ["$set"] = new BsonDocument("Prop1", "$Prop2") }}},
{ "multi", true }
}
}
}
};
database.RunCommand<BsonDocument>(command);
MongoDB 4.2+ Golang
result, err := collection.UpdateMany(ctx, bson.M{},
mongo.Pipeline{
bson.D{{"$set",
bson.M{"name": bson.M{"$concat": []string{"$lastName", " ", "$firstName"}}}
}},
)
从Mongo 4.2开始,db.collection.update()可以接受一个聚合管道,最终允许基于另一个字段更新/创建一个字段:
// { firstName: "Hello", lastName: "World" }
db.collection.updateMany(
{},
[{ $set: { name: { $concat: [ "$firstName", " ", "$lastName" ] } } }]
)
// { "firstName" : "Hello", "lastName" : "World", "name" : "Hello World" }
第一部分{}是匹配查询,过滤要更新的文档(在本例中是所有文档)。 第二部分[{$set: {name:{…}}]是更新聚合管道(注意方括号表示使用聚合管道)。$set是一个新的聚合操作符,是$addFields的别名。
在MongoDB 4.2+版本中,更新更加灵活,因为它允许在更新、updateOne和updateMany中使用聚合管道。你现在可以使用聚合操作符转换你的文档,然后更新,而不需要显式地声明$set命令(而不是使用$replaceRoot: {newRoot: "$$ROOT"})
在这里,我们使用聚合查询从MongoDB的ObjectID“_id”字段提取时间戳,并更新文档(我不是SQL专家,但我认为SQL不提供任何自动生成的ObjectID,有时间戳,你必须自动创建该日期)
var collection = "person"
agg_query = [
{
"$addFields" : {
"_last_updated" : {
"$toDate" : "$_id"
}
}
},
{
$replaceRoot: {
newRoot: "$$ROOT"
}
}
]
db.getCollection(collection).updateMany({}, agg_query, {upsert: true})