Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

367
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda