我如何能MongoDB的文档中删除嵌套的数组元素与C#驱动程序 [英] How can I delete nested array element in a mongodb document with the c# driver

查看:290
本文介绍了我如何能MongoDB的文档中删除嵌套的数组元素与C#驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB的世界新的,现在我挣扎如何删除,在文档的嵌套数组字段更新元素。这里是我的samle文档:

  {
_id:的ObjectId(55f354533dd61e5004ca5208),
名称:手工制作的产品真实的!,
说明:产品全部由手工制造,
产品:
{
标识 :170220151653,
价格:20.5,
名:皮革手镯,
说明:该手镯是由手工制作,
的ImageUrl:https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQii6JCvXtx0iJGWgpvSl-KrdZONKYzDwS0U8uDvUunjO6BO9Aj
}
]
}

在我的方法,我得到的文件,我想删除产品的ID(标识)的ID。谁能告诉我怎样才能从产品字段删除其标识的元素:170220151653



我试过:

  VAR的查询= Query.And(Query.EQ(_ ID的categoryId),Query.EQ(Products.Identifier的productId)); 

变种更新= Update.Pull(产品,新BsonDocument(){{标识符的productId}});

myDb.Applications()更新(查询,更新);



如下建议:的 MongoDB中删除子文档



子文档文件但我得到一个错误




myDb.Applications()




它只是不能被发现。



解决:



  VAR拉=更新<分类和GT; .Pull(X => x.Products,建设者= GT; builder.EQ(q => q.Identifier,productId参数)); 
collection.Update(Query.And(Query.EQ(_ ID,ObjectId.Parse(的categoryId)),Query.EQ(Products.Identifier的productId)),拉);


解决方案

您正在调用方法拉(字符串名称,MongoDB.Bson.BsonValue值),并根据它的文档




这是移除所有值命名的数组元素等于约
值(见$拉)




和您提供的 {标识符的productId} 作为值。我猜蒙戈没有发现的确切的值。



尝试使用的第二个重载拉与查询条件,而不是精确值




移除符合某些查询
已命名的数组元素的所有值(见$拉)。




  VAR更新= Update.Pull(产品,查询.EQ(标识符的productId)); 



更新



既然你提到类别实体,所以我可以建议使用lambda代替
Query.EQ

  VAR拉=更新<类别> .Pull(X => x.Products,建设者=> 
建设者。凡(q => q.Identifier ==的productId));


I am new in the Mongodb world and now I am struggling of how can I delete, update element in a nested array field of a document. Here is my samle document:

{
    "_id" : ObjectId("55f354533dd61e5004ca5208"),
    "Name" : "Hand made products for real!",
    "Description" : "Products all made by hand",
    "Products" : [ 
        {
            "Identifier" : "170220151653",
            "Price" : 20.5,
            "Name" : "Leather bracelet",
            "Description" : "The bracelet was made by hand",
            "ImageUrl" : "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQii6JCvXtx0iJGWgpvSl-KrdZONKYzDwS0U8uDvUunjO6BO9Aj"
        }
    ]
} 

In my method I get the id of the document and the id(Identifier) of the Product that I want to delete. Can anyone tell me how can I delete from the Products field the element having Identifier: 170220151653 ?

I tried:

var query = Query.And(Query.EQ("_id", categoryId), Query.EQ("Products.Identifier", productId));

            var update = Update.Pull("Products", new BsonDocument(){{ "Identifier", productId }});

            myDb.Applications().Update(query, update);

as suggested here: MongoDB remove a subdocument document from a subdocument

But I get an error at

myDb.Applications()

It just cant be found.

SOLVED:

var pull                = Update<Category>.Pull(x => x.Products, builder => builder.EQ(q => q.Identifier, productId));
            collection.Update(Query.And(Query.EQ("_id", ObjectId.Parse(categoryId)), Query.EQ("Products.Identifier", productId)), pull);

解决方案

You are calling method Pull(string name, MongoDB.Bson.BsonValue value) and according to the docs it

Removes all values from the named array element that are equal to some value (see $pull)

and you provide { "Identifier", productId } as the value. I guess that mongo does not find that exact value.

Try to use the second overload of Pull with query-condition instead of exact value

Removes all values from the named array element that match some query (see $pull).

var update = Update.Pull("Products", Query.EQ("Identifier", productId));

UPDATE

Since you mention Category entity so I can suggest using lambda instead of Query.EQ:

var pull = Update<Category>.Pull(x => x.Products, builder =>
builder.Where(q => q.Identifier == productId));

这篇关于我如何能MongoDB的文档中删除嵌套的数组元素与C#驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆