如何在 AQL 中使用 NEIGHBORS?

How to use NEIGHBORS in AQL?

我是 ArangoDB 的新手并且已经是一个越来越多的粉丝。在许多事情中,我们需要将多对多关系转换为图形,并在其中进行高效查询。 但是我似乎无法重现 the cookbook 中描述的 NEIGHBORS 中的行为 在 "Using Edge Collections".

插入数据后运行:

FOR b IN books RETURN { book: b, authors: NEIGHBORS(books, written, b._id, 'inbound') }
[
  {
    "book" : {
      "_id" : "books/10519631898915",
      "_key" : "10519631898915",
      "_rev" : "10519631898915",
      "title" : "The beauty of JOINS"
    },
    "authors" : [ ]
  }
]

清空作者列表!我试过这个:

FOR b IN books RETURN { book: b, authors: NEIGHBORS(authors, written, b._id, 'inbound') }
[
  {
    "book" : {
      "_id" : "books/10519631898915",
      "_key" : "10519631898915",
      "_rev" : "10519631898915",
      "title" : "The beauty of JOINS"
    },
    "authors" : [
      "authors/10519474612515",
      "authors/10519475792163"
    ]
  }
]

哪个 return 是 _id 列表。 None 的那些 return 我在食谱中需要的东西,这是预期的 edge/vertex 结构。 (2.6.9都测试过了)

NEIGHBORS 的使用目的是什么?我如何在纯 AQL 中实现我的目标? 某处是否有 NEIGHBORS(和其他图形 AQL 功能)的标准文档,其中包含每个参数的描述和类型以及 return 值?

好的,我找到了一个解决方案:

FOR p IN PATHS(books, written, 'inbound') 
RETURN p.destination

结果:

Warnings:

[1577], 'collection 'books' used as expression operand'

Result:

[
  {
    "_id": "books/10519631898915",
    "_rev": "10519631898915",
    "_key": "10519631898915",
    "title": "The beauty of JOINS"
  },
  {
    "_id": "authors/10519474612515",
    "_rev": "10519474612515",
    "_key": "10519474612515",
    "name": {
      "first": "John",
      "last": "Doe"
    }
  },
  {
    "_id": "authors/10519475792163",
    "_rev": "10519475792163",
    "_key": "10519475792163",
    "name": {
      "first": "Maxima",
      "last": "Musterfrau"
    }
  }
]

它至少获得了目标顶点,但它似乎不正确,因为我收到警告并且源顶点被包含为目标。 非常欢迎进一步阐述和建议。

您是否尝试过 includeData NEIGHBORS 选项?

FOR b IN books RETURN { book: b, authors: NEIGHBORS(authors, written, b._id, 'inbound', [], {includeData: true}) }

这在我的测试中有效。 在大型数据集上,它将比 PATHS 的性能更高(PATHS 计算更多不相关的信息)

注意:空数组[] 用于定义仅应遵循的边。对于空数组,我们遵循所有边,但您也可以遵循特殊边 f.e。 {label: "written"} 而不是 [].

更新 (2017):AQL 不再支持 NEIGHBORS 3.x

而不是

NEIGHBORS(books, written, b._id, 'inbound')

你可以写一个子查询:

(FOR v IN 1..1 INBOUND b written RETURN v)