I have a series of ObjectIds and I want to update a property in all documents where there's a match with the ObjectIds. So basically the SQL equivalent of:
update collection set prop = false where id in (1,2,3)
The Query I have built for Mongo is:
List<BsonValue> output = GetValues();
var builder = Builders<MyCollection>.Filter;
var filter = builder.In("_id", output);
var update = Builders<MyCollection>.Update.Set("Prop", false);
var result = myCollection.UpdateMany(filter, update);
However, I get the following exception when I run:
Specified cast is not valid.
Any idea what I'm doing wrong? Thanks in advance!
UPDATE
So this query was fine... looks like the problem was with my class.
My class was:
public class MyCollection
{
[BsonId]
public ObjectId _id { get; set; }
//Other properties
}
Apparently the ObjectId was not being mapped correctly. I didn't have an explicit need for it so I removed it and it started working thereafter.
Hope this helps someone else.
Answering my own question so I can help someone else in the future.
my class was:
public class MyCollection { [BsonId] public ObjectId _id { get; set; } } //Other properties }The ObjectId property was not being assigned correctly. In my case I didn't need it so I removed it and it started working from then on.