更新或替换MongoDB集合中的嵌入式文档 [英] Update or replace an embedded document in MongoDB collection
本文介绍了更新或替换MongoDB集合中的嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下数据结构:
db.objects
{
"_id" : 1,
"name" : "object name",
"type" : "rectangle",
"area" : {
"position_x1" : 0,
"position_y1" : 0,
"position_x2" : 0,
"position_y2" : 0
},
"dimension" : {
"width" : 0,
"height" : 0
}
}
我想用"_id"
= 1分别替换/更新对象集合中的文档的"area"
和"dimension"
.
I want to replace/update "area"
and "dimension"
separately for a document in objects collection with "_id"
= 1.
[?]请让我知道如何使用MongoDB C#驱动程序.
[?] Please let me know how can i achieve using MongoDB C# driver.
推荐答案
This is a very simple use of the $set
operator. So essentially the update form is:
db.objects.update(
{ "_id": 1 },
{
"$set": {
"area": { /* all of your object properties */ },
"dimension": { /* all of your object properties */ }
}
}
)
或者,您分别进行操作:
Alternately you do them separately:
db.objects.update(
{ "_id": 1 },
{
"$set": {
"area.position_x1": 1,
"area.position_y1": 1,
"dimension.width": 1
}
}
)
根据您的实际需求.
或作为带有Builder的C#类型代码:
Or as C# type code with a Builder:
var query = Query.EQ("_id",1);
var update = Update.Set("area.position_x1", 1)
.Set("area.position_y1", 1)
.Set("dimension.width", 1);
MongoCollection<Class>.update(query, update);
这篇关于更新或替换MongoDB集合中的嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文