How to retrieve a subset of fields using the C# MongoDB driver?

前端 未结 2 1648
忘了有多久
忘了有多久 2021-01-04 01:58

I\'ve searched the world over and can\'t seem to find the answer to this.

How do I do this in C#:

// retrieve ssn field for documents where last_name         


        
相关标签:
2条回答
  • 2021-01-04 02:28

    Note that you can now use a (type/refactoring)-safe version:

    usersCollection.FindAllAs<User>()
                   .SetFields(Fields<User>.Include(user => user.FirstName,
                                                   user => user.LastName)
                                          .Exclude(user => user.SSN)
                   .ToArray();
    
    0 讨论(0)
  • 2021-01-04 02:40

    To include:

    .SetFields(Fields.Include("first_name", "last_name"));
    

    To exclude fields:

    .SetFields(Fields.Exclude("SSN","Salary"));
    

    To do both:

    .SetFields(Fields.Include("first_name", "last_name").Exclude("SSN","Salary"));
    
    0 讨论(0)
提交回复
热议问题