MongoDB removes a subdocument document from a subdocument

I am using the 10gen C # driver for MongoDB and I would like to remove the subdocument from the subdocument. I do not know how to do that.

Here is an example of what looks like my document

{ "_id": "binary_stuff", "Name": "MyApplication", "Settings": [ { "_id": "binary_stuff", "Key": "ImportDirectory", "Value": "C:\data", "Overrides": [{ "_id": "binary_stuff", "Name": "PathDirectory", "Value": "C:\anotherData" }] }, } 

And I want to remove the overridden name, which is PathDirectory. Here is the request that I wrote, but it does not work. I have no mistake.

 var query = Query.And(Query.EQ("_id", applicationId), Query.EQ("Settings.Key", "ImportDirectory"), Query.EQ("Settings.$.Overrides.Name", "PathDirectory")); Run(database => database.Applications().Remove(query)); 

Thanks for any help. John

+4
source share
1 answer

you need to use the $ pull operation to remove the element from the array.

  var query = Query.And(Query.EQ("_id", applicationId), Query.EQ("Settings.Key", "ImportDirectory")); var update = Update.Pull("Settings.$.Overrides", new BsonDocument(){ { "Name", "PathDirectory" } }); database.Applications().Update(query, update); 
+8
source

Source: https://habr.com/ru/post/982687/


All Articles