[英]How can I update mongodb document for adding a new item to array?
我無法找出插入子數組...
我想將項目插入MyArray ...
我的更新文件應該如何?
MyCollection.Update(
new QueryDocument { { "_id", MyObject.Id } },
new UpdateDocument { { "$set", new BsonDocument { { "MyArray",
new BsonArray { new BsonDocument {{ "ArrayItemId", myArrayField.Id }},
new BsonDocument {{ "Name", myArrayField.Name }} }}}}},
UpdateFlags.None);
新MongoDB c#異步適配器的語法:
var filter = Builders<myObject>
.Filter.Eq(e => e.Name, "name");
var update = Builders<myObject>.Update
.Push<String>(e => e.MyArray, myArrayField);
await collection.FindOneAndUpdateAsync(filter, update);
使用$push
運算符完成數組插入。
作為旁注,您不需要使用QueryDocument
和UpdateDocument
。 有一個更簡單的幫助語法:
MyCollection.Update(Query.EQ("_id", MyObject.Id),
Update.PushWrapped("MyArray", myArrayField)
請注意, PushWrapped<T>
允許推送文檔,而Push
只接受可以由MongoDB中的簡單字段表示的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.