How can I delete nested array element in a mongodb document with the c# driver

喜夏-厌秋 提交于 2019-12-03 14:37:31

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));

I was also facing the same problem and then finally after doing lot of R&D, I came to know that, you have to use PullFilter instead of Pull when you want to delete using filter.

Hi as per my understanding you want to remove whole matched elements of given id and identifier so below query will may solve your problem but I don't know how to convert this into C#, here mongo $pull method use.

    db.collectionName.update({"_id" :  
        ObjectId("55f354533dd61e5004ca5208")},
        {"$pull":{"Products":{"Identifier":"170220151653"}}})
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!