MongoDB Driver Fields Exclude

Question:

My problem is this, I have an object:

public class Obj(){
    prop int id {get; set;}
    prop Client client {get; set;}
}

public class Client(){
   prop int id {get; set;}
   prop Group group {get; set;}
}

public class Group(){
   prop int id {get; set;}
}

When performing the query, I would like to return only some object values:

var lRetorno = collection.Find(Query.And(query))
                .SetFields(Fields<Obj>.Include(c => c.id, c => c.Client)
                .Exclude(c => c.Client.Group))

Is there a way to return the Client obj without the Group object mapping, without changing the class mapping, too?

Answer:

I'm not directly familiar with C#, but what you need to check is support for your driver's projections.

In the quick tour of the mongo c# driver documentation there is a section on projection, which mentions a projection builder . These two places should help you.

Scroll to Top
AllEscort