将Mongodb聚合管道转换为C#.Net Core

时间:2018-09-18 13:51:30

标签: c# .net mongodb aggregation

也许有人可以帮助我解决使用mongodb .Net驱动程序将mongodb聚合查询转换为C#的问题。

根据我的问题here,我尝试将以下内容转换为C#

db.getCollection('test').aggregate([
    { "$facet": {
        "allInRoot1": [{
            "$match": { "rootReferenceId": LUUID("9f3a73df-bca7-48b7-b111-285359e50a02") }
        }],
        "allInRoot2": [{
            "$match": { "rootReferenceId": LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9") }
        }]
    }},
    { "$project": {
        "difference": {
            "$filter": {
                "input": "$allInRoot1",
                "as": "this",
                "cond": { "$in": [ "$$this.reference.id", { "$setDifference": [ "$allInRoot1.reference.id", "$allInRoot2.reference.id" ] } ] }
            }
        }
    }}
])

到目前为止,我有这个

        var matchFilterOne = new ExpressionFilterDefinition<NodeModel>(node => node.RootReferenceId == baseId);
        var matchStageOne  = PipelineStageDefinitionBuilder.Match(matchFilterOne);
        var pipelineOne = PipelineDefinition<NodeModel, NodeModel>.Create(new IPipelineStageDefinition[] { matchStageOne });

        var matchFilterTwo = new ExpressionFilterDefinition<NodeModel>(node => node.RootReferenceId == idToExclude);
        var matchStageTwo = PipelineStageDefinitionBuilder.Match(matchFilterTwo);
        var pipelineTwo = PipelineDefinition<NodeModel, NodeModel>.Create(new IPipelineStageDefinition[] { matchStageTwo });

        var facetPipelineOne = AggregateFacet.Create("allInRoot1", pipelineOne);
        var facetPipelineTwo = AggregateFacet.Create("allInRoot2", pipelineTwo);
        var test = testCollection.Aggregate()
            .Facet(facetPipelineOne, facetPipelineTwo)
            /* This seems to fail because the facet structure is wrong and it can't access the $allInRoot1 field ...

            .Project(@"{
                'difference': {
                    '$filter': {
                        'input': '$allInRoot1',
                        'as': 'this',
                        'cond': {
                            '$in': [ '$$this.reference.id', { '$setDifference': [ '$allInRoot1.reference.id', '$allInRoot2.reference.id' ] }]
                        }
                 }}}")
            */
        .FirstOrDefault();

也许有人有一个线索指向我正确的方向?也可以将投影与类型一起使用吗?

感谢您的帮助!

0 个答案:

没有答案