【问题标题】:Convert Mongodb Aggregation Pipeline to C# .Net Core将 Mongodb 聚合管道转换为 C# .Net Core
【发布时间】:2019-02-22 14:01:00
【问题描述】:

也许有人可以帮助我解决使用 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();

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

感谢任何帮助!

【问题讨论】:

  • 使用 c# 创建一个 mongo 集合然后你可以使用 Linq 或 Lambda 表达式进行查询
  • 哦,我已经有一个 mongo 集合并且正在使用它。在代码中是 testCollection。
  • 您似乎擅长 mongo 查询,在这种情况下,您无需将查询转换为 Linq 或 lambda 表达式。
  • 是的,你是对的。也许我应该按原样使用查询。大多数类型无论如何都不能很好地工作。例如。投影阶段内的 AggregatedFacetResults。我不确定它是否兼容。

标签: c# .net mongodb aggregation


【解决方案1】:

到目前为止,我见过的最好的解决方案是将整个聚合数组复制到 MongoDbCompass 的聚合选项卡中,然后将其导出到 C# 中。

【讨论】:

  • 这个答案并没有真正回答原始问题。我建议在这里阅读 Stack Overflow 答案指南:stackoverflow.com/help/how-to-answer。对于这个特定问题,如果不可能,您的答案应该解释这一点并解释解决方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
相关资源
最近更新 更多