![](/img/trans.png)
[英]Upsert a complete document (replace all if "Id" exists // insert if "Id" doesn't exists ) with C# Driver (MongoDB)
[英]C# How to replace document by id
有没有办法用ID替换mongodb上的文档? 我不想通过某些属性来查找文档,而是希望将其替换为ID。 使用指定的具有相同ID的文档。 这可能吗?
您可以使用ReplaceOne
或ReplaceOneAsync
来做到这一点:
var filter = Builders<BsonDocument>.Filter
.Eq("_id", new ObjectId("561674ef936e327431cbd349"));
var newdoc = new BsonDocument
{
// _id is optional here, but if it's present, it must match the replaced doc's _id
{"_id", new ObjectId("561674ef936e327431cbd349")},
{"label", "new value"}
};
var result = collection.ReplaceOne(filter, newdoc);
请在此处查看有关该主题的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.