[英]Update or replace an embedded document in MongoDB collection
我有以下數據結構:
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"
。
[?]請讓我知道如何使用MongoDB C#驅動程序。
這是$set
運算符的非常簡單的用法。 因此,實質上,更新形式為:
db.objects.update(
{ "_id": 1 },
{
"$set": {
"area": { /* all of your object properties */ },
"dimension": { /* all of your object properties */ }
}
}
)
或者,您分別進行操作:
db.objects.update(
{ "_id": 1 },
{
"$set": {
"area.position_x1": 1,
"area.position_y1": 1,
"dimension.width": 1
}
}
)
根據您的實際需要。
或作為帶有Builder的C#類型代碼:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.