如何在 ArangoDB 中使用 self Join 访问集合中多个文档的数据

How to access the data from multiple documents within a collection with self Join in ArangoDB

我已经将数据存储在 ArangoDB 2.7.1 中,集合名称为 DSP:

{"content": "Book.xml", "type": "string", "name": "name", "key": 102}
{"content": "D:/XMLexample/Book.xml", "type": "string", "name": "location", "key": 102}
{"content": "xml", "type": "string", "name": "mime-type", "key": 102}
{"content": 4130, "type": "string", "name": "size", "key": 102}
{"content": "Sun Aug 25 07:53:32 2013", "type": "string", "name": "created_date", "key": 102}
{"content": "Wed Jan 23 09:14:07 2013", "type": "string", "name": "modified_date", "key": 102}
{"content": "catalog", "type": "tag", "name": "root", "key": 102}
{"content": "book", "type": "string", "name": "tag", "key": 103} 
{"content": "bk101", "type": {"py/type": "__builtin__.str"}, "name": "id", "key": 103}
{"content": "Gambardella, Matthew", "type": {"py/type": "__builtin__.str"}, "name": "author", "key": 1031} 
{"content": "XML Developer's Guide", "type": {"py/type": "__builtin__.str"}, "name": "title", "key": 1031}
{"content": "Computer", "type": {"py/type": "__builtin__.str"}, "name": "genre", "key": 1031}
{"content": "44.95", "type": {"py/type": "__builtin__.str"}, "name": "price", "key": 1031}
{"content": "2000-10-01", "type": {"py/type": "__builtin__.str"}, "name": "publish_date", "key": 1031}
{"content": "An in-depth look at creating applications with XML.", "type": {"py/type": "__builtin__.str"}, "name": "description", "key": 1031}

此处,单个集合 {"content": "Book.xml", "type": "string", "name": "name", "key": 102} 表示集合中的单个文档。

现在,我想访问多个文档中 key 属性值相似的所有文档,或者 genre 是 computer 的值 title 和 price 的所有文档”以获得相同的 key 值。我已将 AQL 尝试为 FOR p IN DSP filter p.name == "publish_date" AND p.content == "2000-10-01" AND p.name == 'title' return p,但这返回了一个空集,因为它是在单个文档中进行比较,而不是在集合中进行比较。

与关系数据库一样,需要某种自连接,但我不知道如何应用自连接。请告诉我如何访问具有相同键属性值的所有文档,其中 publish_date 是“2000-10-01”。我希望此查询的结果是以下文档,因为对应于 publish_date 的值为 2000-10-01 key 的值为 1031:

{"content": "Gambardella, Matthew", "type": {"py/type": "__builtin__.str"}, "name": "author", "key": 1031} 
{"content": "XML Developer's Guide", "type": {"py/type": "__builtin__.str"}, "name": "title", "key": 1031}
{"content": "Computer", "type": {"py/type": "__builtin__.str"}, "name": "genre", "key": 1031}
{"content": "44.95", "type": {"py/type": "__builtin__.str"}, "name": "price", "key": 1031}
{"content": "2000-10-01", "type": {"py/type": "__builtin__.str"}, "name": "publish_date", "key": 1031}
{"content": "An in-depth look at creating applications with XML.", "type": {"py/type": "__builtin__.str"}, "name": "description", "key": 1031}

假设发布日期存储在属性 name 中,其值存储在属性 content 中,您首先需要找到具有该组合的所有文档:

FOR self IN DSP 
  FILTER self.name == 'publish_date' && self.content == '2000-10-01'
  RETURN self

现在,找到这些文档后,您可以再次将它们加入 DSP 集合,过滤掉具有相同 key 值的文档,但排除初始 [=15= 中已找到的文档]:

FOR self IN DSP 
  FILTER self.name == 'publish_date' && self.content == '2000-10-01'
  FOR other IN DSP 
    FILTER other.key == self.key && other._key != self._key 
    RETURN { self, other }

如果您总是根据名称和内容 and/or 键进行过滤,那么为这些属性编制索引可能是明智的。看起来 key 值得一个单独的索引。哈希索引应该足够了,因为 key 将始终是 equality-compared。 namecontent(按此顺序)可以放入 skiplist 索引。