首页 > 解决方案 > 选择具有嵌入文档的过滤值的文档

问题描述

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

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

如果匹配_id,我正在尝试选择一个文档,并在对嵌入式文档应用过滤器后检索结果。它在 Robo3T 上的 MongoDB 中运行良好。但我无法在 C# 中翻译相同的内容。

标签: c#mongodbaggregation

解决方案


这应该可以帮助您:

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") });

推荐阅读