首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongodb graphLookup

Mongodb graphLookup
EN

Stack Overflow用户
提问于 2016-12-06 15:13:25
回答 1查看 9.2K关注 0票数 4

由于MongoDB最近引入了graphLookup,我一直在尝试寻找它是否可以容纳一个简单的社会关系图。我目前使用neo4j就是为了这个目的。

我将graphLookup理解为一种递归搜索,它只是通过每个文档的“connectFromField”更深入地搜索。

虽然我能够做基本的事情,但我想为每个关系提供更多的属性。例如,这里的第一个示例:(Employees and reporting hierarchy)

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

代码语言:javascript
复制
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }

如果我需要向'reportsTo‘值添加一个开始日期,如下所示:

代码语言:javascript
复制
{ "_id" : 2, "name" : "Eliot", "reportsTo" : {"name": "Dev", "from": "date"  } }

恐怕这是不受支持的。

我想知道是否有人这样使用过MongoDB。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-06 17:50:23

假设我们插入了以下文档:

代码语言:javascript
复制
> 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 ] }

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

代码语言:javascript
复制
db.employees.aggregate([
{
   $graphLookup: {
      from: "employees",
      startWith: "Eliot",
      connectFromField: "reportsTo.name",
      connectToField: "name",
      as: "reportingHierarchy"
   }
}
])

然后,它将返回以下结果:

代码语言:javascript
复制
{
        "_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")
                        }
                }
        ]
}

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

代码语言:javascript
复制
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/

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40989763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档