Mongodb Driver Fields Exclude

Asked

Viewed 116 times

3

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

There is a way to return obj Client without mapping the object Group, without changing the class mapping, including?

1 answer

1

I don’t know C#directly, but what you need to check is the support for your driver projections.

In the Quick tour from the documentation of the c# driver of Mongo there is a section on projection, which mentions a Builder of projections. These two places should help you.

  • I’ve read all the documentation. If you look at it yourself, none of the examples show the actions on the second level of the object. And it is exactly my problem, after the first level. No failure occurs in the example of the post, it just doesn’t work. Returns everything e. =T

  • I got it. I found this question in the stackoverflow (in English) the reply comments that the projections are made on the client (with LINQ). Are you using this? Since you do not have lists in your objects it is worth considering that it is not so expensive to search for your object with all dependencies, this is a document only in the bank?

  • The consultations are done with lambda, and are very considerable consultations. In the post example I only used aggregated object but in the application there are many levels of objects and aggregated lists. Have you ever imagined consumption without need? = T

Browser other questions tagged

You are not signed in. Login or sign up in order to post.