Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

365
Views
UpdateMany operation specifying filter as _id in

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!