Search code examples
c#linqmongodbmongodb-.net-driver

Mongo c# Driver Query (Select subfields)


folowing situation: I have a list of Users, evry one have a field with a List of Comments.

User1: {
      ...
      Id : 'xxxx',
      Comment : [{
                   ...
                   Status : 1
                }
                ,{
                   ...
                   Status : 0
                }]
}

I am looking for a Mongo c# Query that select all Comments with status 1 of all users in the DB Collection.

sry for bad English.

thx


Solution

  • thx.

    I slove it like this:

    var query1  =  Query.EQ("Comments.Status", 1)
    IEnumerable<Comment> comments = Collection.Distinct<Comment>("Comments", query1).Where(x => x.Status == 1);
    
    comments .toList() // <= list of Comments with Status 1
    

    if someone has a better solution please post it.

    Thx again,

    Benjamin