Reputation: 5162
Given below data, I want to delete all sub-documents in "data"
where "k" == "c"
. That's my code:
var query = Query.EQ("templateId", "537c6648d7d6bd0a68a34918");
var update = Update.Pull("data", new BsonDocument() { { "k", "c" } });
MyMongoCollection.Update(query, update);
So what happens is that exactly 1 document is affected and the updating stops. I verified the query and it returns all documents as expected. Just with the pull-update, it breaks.
What could be wrong?
WriteConcern has no error. The query alone returns 3 docs and the WriteConcern has 1 doc affected.
{
"_id" : ObjectId("537c85a2d7d6bd0a68a3491b"),
"templateId" : "537c6648d7d6bd0a68a34918",
"data" : [
{"k" : "b", "v" : "111"},
{"k" : "a", "v" : "222"},
{"k" : "c", "v" : "333"}
]
},
{
"_id" : ObjectId("537d6a41d7d6bd0608cb27a4"),
"templateId" : "537c6648d7d6bd0a68a34918",
"data" : [
{"k" : "b", "v" : "111"},
{"k" : "a", "v" : "222"},
{"k" : "c", "v" : "333"}
]
}
Upvotes: 1
Views: 609
Reputation: 12240
For updating multiple documents you should set the update flags to Multi
:
MyMongoCollection.Update(query, update, UpdateFlags.Multi);
This will remove subdocuments from all documents.
From the MongoDB documentation:
By default, the update() method updates a single document that matches its selection criteria. Call the method with the multi option set to true to update multiple documents.
Upvotes: 1