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

320
Visualizações
Ignore field on read (deserialization) with C# SDK

I have a document with two fields; name and documents.

I want both fields to be serialized and inserted into my collection during writing. BUT, I do not want documents to be deserialized during reading (I am happy with this field being null),

Here is the entity class:

public class EmployeeDocument
{
    [BsonElement("name")]
    public string Name { get; set; }

    [BsonElement("documents")]
    public List<string> Documents { get; set; }
}

How could I achieve this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think it's not possible to ignore a property for deserialization, but not for serialization. And i think it's not the best approach. If you want to read not all properties from the document, you could exclude them on projection, if you than want to have the same EmployeeDocument class, you could deserialize your BsonDocument to it, i mean something like:

var emps = db.GetCollection<EmployeeDocument>("empl");
var employees =emps.Find(x=>true)
             .Project(Builders<EmployeeDocument>.Projection.Exclude(x=>x.Documents))
             .ToList()
             .Select(x=>BsonSerializer.Deserialize<EmployeeDocument>(x));
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