将许多字段传递到聚合管道的下一个阶段

时间:2018-10-25 09:35:21

标签: c# mongodb aggregation-framework

请让我知道下面的MongoDB查询的C#等效项。在MongoDB的Robo3T中运行正常

db.UserProfile.aggregate([          
    {
      $project: {
         SName:1,
         FName:1,
         DemRole: {
            $filter: {
               input: "$DemRole",
               as: "item",
               cond: { $eq: [ "$$item.Name", "FO" ] }
            }
         }
      }
   }
])

1 个答案:

答案 0 :(得分:0)

我可以通过下面的C#代码实现

var stage1 = "{ $project: { SName: 1, FName: 1, DemRole: { $filter: { input: '$DemRole', as: 'item', cond: { $eq: ['$$item._id', '5bc49241f12541c3aaa098ee'  ] } } } } }";


                var query = collection.Aggregate(new AggregateOptions() { AllowDiskUse = true })

                            .AppendStage<BsonDocument>(stage1).ToEnumerable();



                return query?.Select(point => BsonSerializer.Deserialize<RetUserProfile4Role>(point)).ToList();