Remove version 2.4 of mongodb from the subdocument by id

This is my document.

Post{ "_id" : 1, "Code" : CSUUID("ba22a2a3-e6b5-4ce6-a3ad-20e5196cca46"), "Zip" : 123456, "Text" : "Hello", "Tags" : [{ "_id" : 1, "Tag" : "Tag8" }, { "_id" : 2, "Tag" : "Tag9" }, { "_id" : 3, "Tag" : "Tag10" }] } 

I need to remove one of the "tags" by id in C #. for example: delete from post where tag.id = 2

+1
source share
2 answers

MongoDB.Bson Version: 2.0.1.27

MongoDB.Driver version: 2.2.0.262

MongoDB.Driver.Code Version: 2.2.0.262

 const int id = 1; var pull = Builders<Post>.Update.PullFilter(x => x.Tags, a => a.Id == id); var filter1 = Builders<Post>.Filter.And(Builders<Post>.Filter.Eq(a => a.Id, 1), Builders<Post>.Filter.ElemMatch(q => q.Tags, t => t.Id == id)); collection.UpdateOneAsync(filter1, pull); 
+1
source

I have found the answer.

 var pull = Update<Post>.Pull(x => x.Tags, builder => builder.EQ(q => q.Id, 2)).ToBsonDocument(); var filter1 = Builders<Post>.Filter.And(Builders<Post>.Filter.Eq(a => a.Id, 1), Builders<Post>.Filter.ElemMatch(q => q.Tags, t => t.Id == 2)); collection.UpdateOneAsync(filter1, pull); 
0
source

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


All Articles