Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

366
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda