使用AggregateFluentExtensions的MongoDB graphLookup

时间:2018-10-05 16:35:25

标签: c# mongodb aggregation-framework

我一直在尝试使用最新的C#驱动程序执行MongoDB graphLookup,该驱动程序使用 AggregateFluentExtensions 。根据{{​​3}}: 该方法收到一系列我无法找到工作方法的参数。

有人使用过它,可以帮我举个例子吗?

这是我聚合的json版本:

db.getCollection("Item").aggregate(
    [
        { 
            "$project" : {
                "itemMasterId" : 1.0, 
                "parentItemId" : 1.0
            }
        }, 
        { 
            "$graphLookup" : {
                "from" : "Item", 
                "startWith" : "$itemMasterId", 
                "connectFromField" : "itemMasterId", 
                "connectToField" : "parentItemId", 
                "as" : "ancestors", 
                "maxDepth" : 10, 
                "depthField" : "depthField", 
                "restrictSearchWithMatch" : {
                    "locationId" : 26
                }
            }
        }, 
        { 
            "$project" : {
                "itemMasterId" : 1.0, 
                "parentItemId" : 1.0, 
                "children.itemMasterId" : 1, 
                "children.parentItemId" : 1
            }
        }
    ]
);

谢谢!

1 个答案:

答案 0 :(得分:0)

(晚点聚会,希望对别人有帮助。)

我使用了AppendStage()方法,您可以尝试以下方法:

var graphLookupStage = new BsonDocument("$graphLookup",
                        new BsonDocument
                            {
                                    { "from", "someCollection" },
                                    { "startWith", "$reportsTo" },
                                    { "connectFromField", "reportsTo"},
                                    { "connectToField",  "name" },
                                    { "as", "reportingHierarchy" },
                                    { "maxDepth", 1 },
                                    { "depthField", "depthField" } //optional
                        });
var result = collection.Aggregate().AppendStage<BsonDocument>(graphLookupStage);