I want to remove one field from every document using MongoDB driver for C#.
I have documents like this:
{
"_id": ObjectId("554e05dfc90d3d4dfcaa2aea"),
"username": "abc",
"someArray": [
{
"_id": ObjectId("554e0625a51586362c33c6df"),
"IsReadOnly": false
},
{
"_id": ObjectId("554e0625a51586362c33c6df"),
"IsReadOnly": true
}
]
}
I want to remove the field "IsReadOnly" from "someArray", regardless of whether the value is true or false.
In MongoDB I can reach that with the following script:
db.getCollection('Collection').find({}).forEach( function(doc) {
var arr = doc.someArray;
var length = arr.length;
for (var i = 0; i < length; i++) {
delete arr[i]["IsReadOnly"];
}
db.getCollection('Collection').save(doc);
});
In C# I tried the following:
var update= Update.Pull("someArray",BsonValue.Create("IsReadOnly"));
// or: var update = Update.Unset("someArray.$[].IsReadOnly");
myDb.Collection.Update(null, update, UpdateFlags.Multi);
But the "IsReadOnly" wasn't deleted. How can I delete this field from every array and every document?