繁体   English   中英

Mongo c# 驱动程序查询(选择子字段)

[英]Mongo c# Driver Query (Select subfields)

以下情况:我有一个用户列表,每个用户都有一个带有评论列表的字段。

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

我正在寻找一个 Mongo c# 查询,它选择数据库集合中所有用户的状态为 1 的所有评论。

sry 英语不好。

谢谢

假设您有以下包含集合的序列化值的类:

public class User1
{
    public string Id { get; set; }

    public Comment[] Comments { get; set; }
}

public class Comment
{
   public int Status { get; set; }
}

那么查询应该是这样的:

var query =
    collection.AsQueryable<User1>().SelectMany(user => user.Comments.Where(com=>com.Status == 1));

谢谢。

我喜欢这样:

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

如果有人有更好的解决方案,请发布。

再次感谢,

本杰明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM