Mongodb graphLookup

时间:2016-12-06 07:13:25

标签: mongodb aggregation-framework

由于MongoDB最近引入了graphLookup,我试图找出是否可以保存一个简单的社交关系图。我目前正在使用neo4j。

我理解graphLookup是一种递归搜索,它只是通过“ConnectFromField”进行更深入的搜索。每个文件。

虽然我能够做基本的东西,但我想为每个关系提供更多属性。例如,这里的第一个例子是:(员工和报告层次结构)

https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/

{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }

如果我需要在' reportsTo'中添加开始日期价值,像这样:

{ "_id" : 2, "name" : "Eliot", "reportsTo" : {"name": "Dev", "from": "date"  } }

我担心这不受支持。

我想知道是否有人以这种方式使用过MongoDB。

1 个答案:

答案 0 :(得分:6)

说我们已插入以下文件:

> db.employees.insertMany([
... { "_id" : 1, "name" : "Dev" },
... { "_id" : 2, "name" : "Eliot", "reportsTo" : { name: "Dev", "from": ISODate("2016-01-01T00:00:00.000Z") } },
... { "_id" : 3, "name" : "Ron", "reportsTo" : { name: "Eliot", "from": ISODate("2016-01-01T00:00:00.000Z") } },
... { "_id" : 4, "name" : "Andrew", "reportsTo" : { name: "Eliot", "from": ISODate("2016-01-01T00:00:00.000Z") } },
... { "_id" : 5, "name" : "Asya", "reportsTo" : { name: "Ron", "from": ISODate("2016-01-01T00:00:00.000Z") } },
... { "_id" : 6, "name" : "Dan", "reportsTo" : { name: "Andrew", "from": ISODate("2016-01-01T00:00:00.000Z") } },
... ]);
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4, 5, 6 ] }

然后,我们可以使用.使用以下聚合查询从嵌入文档中获取字段:

db.employees.aggregate([
{
   $graphLookup: {
      from: "employees",
      startWith: "Eliot",
      connectFromField: "reportsTo.name",
      connectToField: "name",
      as: "reportingHierarchy"
   }
}
])

然后会返回以下结果:

{
        "_id" : 1,
        "name" : "Dev",
        "reportingHierarchy" : [
                {
                        "_id" : 1,
                        "name" : "Dev"
                },
                {
                        "_id" : 2,
                        "name" : "Eliot",
                        "reportsTo" : {
                                "name" : "Dev",
                                "from" : ISODate("2016-01-01T00:00:00Z")
                        }
                }
        ]
}
{
        "_id" : 2,
        "name" : "Eliot",
        "reportsTo" : {
                "name" : "Dev",
                "from" : ISODate("2016-01-01T00:00:00Z")
        },
        "reportingHierarchy" : [
                {
                        "_id" : 1,
                        "name" : "Dev"
                },
                {
                        "_id" : 2,
                        "name" : "Eliot",
                        "reportsTo" : {
                                "name" : "Dev",
                                "from" : ISODate("2016-01-01T00:00:00Z")
                        }
                }
        ]
}
{
        "_id" : 3,
        "name" : "Ron",
        "reportsTo" : {
                "name" : "Eliot",
                "from" : ISODate("2016-01-01T00:00:00Z")
        },
        "reportingHierarchy" : [
                {
                        "_id" : 1,
                        "name" : "Dev"
                },
                {
                        "_id" : 2,
                        "name" : "Eliot",
                        "reportsTo" : {
                                "name" : "Dev",
                                "from" : ISODate("2016-01-01T00:00:00Z")
                        }
                }
        ]
}
{
        "_id" : 4,
        "name" : "Andrew",
        "reportsTo" : {
                "name" : "Eliot",
                "from" : ISODate("2016-01-01T00:00:00Z")
        },
        "reportingHierarchy" : [
                {
                        "_id" : 1,
                        "name" : "Dev"
                },
                {
                        "_id" : 2,
                        "name" : "Eliot",
                        "reportsTo" : {
                                "name" : "Dev",
                                "from" : ISODate("2016-01-01T00:00:00Z")
                        }
                }
        ]
}
{
        "_id" : 5,
        "name" : "Asya",
        "reportsTo" : {
                "name" : "Ron",
                "from" : ISODate("2016-01-01T00:00:00Z")
        },
        "reportingHierarchy" : [
                {
                        "_id" : 1,
                        "name" : "Dev"
                },
                {
                        "_id" : 2,
                        "name" : "Eliot",
                        "reportsTo" : {
                                "name" : "Dev",
                                "from" : ISODate("2016-01-01T00:00:00Z")
                        }
                }
        ]
}
{
        "_id" : 6,
        "name" : "Dan",
        "reportsTo" : {
                "name" : "Andrew",
                "from" : ISODate("2016-01-01T00:00:00Z")
        },
        "reportingHierarchy" : [
                {
                        "_id" : 1,
                        "name" : "Dev"
                },
                {
                        "_id" : 2,
                        "name" : "Eliot",
                        "reportsTo" : {
                                "name" : "Dev",
                                "from" : ISODate("2016-01-01T00:00:00Z")
                        }
                }
        ]
}

然后我们还可以使用聚合管道的其余部分来执行任何其他操作:

db.employees.aggregate([

   { $match: { "reportsTo.from": { $gt: ISODate("2016-01-01T00:00:00Z") } } },
   { $graphLookup: { ... } },
   { $project: { ... }
]);

有关管道阶段,请参阅https://docs.mongodb.com/v3.2/reference/operator/aggregation-pipeline/