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

318
Views
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 answers
Answer question

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