选择具有嵌入文档过滤值的文档

selecting a document with filtered values of embedded documents

你能指导我如何使 C# 等效

db.UserProfile.aggregate([
    {$match:{_id:"sen"}},  
    {
          $project: {
             DemRole: {
                $filter: {
                   input: "$DemRole",
                   as: "item",
                   cond: { $eq: [ "$$item.Name", "CO" ] }
                }
             }
          }
       }
    ])

我正在尝试 select 一个文档,如果匹配 _id 并在对嵌入式 documents.It 应用过滤器后检索结果,在 Robo3T 上的 MongoDB 中工作正常。但是我无法在 C# 中翻译相同的内容。

这应该让你继续:

var collection = new MongoClient().GetDatabase("test").GetCollection<User>("UserProfile");
var pipeline = collection.Aggregate()
                         .Match(up => up.Id == "sen")
                         .Project(up => new { DemRole = up.DemRole.Where(c => c.Name == "CO") });

感谢您的回答。它现在在 C# 中工作。但下面是我如何获得信息。

 var pipeline = collection.Aggregate()
                         .Match(up => up.UserID == userId)
                         .Project(up => new { DemRole = up.DemRole.Where(c => c.Status== true) }).ToList();
              // .Project(dem => dem.DemRole.Where(c => c.Status == true));//.ToList();


        foreach(var pipe in pipeline)
        {
            return pipe.DemRole.ToList();

        }

我想知道下面代码执行的操作

.Project(up => new { DemRole = up.DemRole.Where(c => c.Status== true) })

为什么下面的代码不起作用。

  .Project(dem => dem.DemRole.Where(c => c.Status == true));//.ToList();

此外,我必须 运行 一个 foreach 来获取下面给出的信息。

foreach(var pipe in pipeline)
        {
            return pipe.DemRole.ToList();

        }

如果你能解释以上几行或者给我指出一些我可以阅读的文档,那就更好了。