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

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

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 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