【问题标题】:How to access the data from multiple documents within a collection with self Join in ArangoDB如何在 ArangoDB 中使用 self Join 访问集合中多个文档的数据
【发布时间】:2016-01-19 19:10:35
【问题描述】:

我已将数据存储在 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 属性的相似值或类型为计算机的值标题和价格的所有文档,以获取相同的 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”。我希望此查询的结果是以下文档,因为对应于值为 2000-10-01 的 publish_datekey 的值为 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}

【问题讨论】:

    标签: arangodb aql


    【解决方案1】:

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

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

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

    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 }
    

    如果您总是根据名称和内容和/或键进行过滤,那么将这些属性编入索引可能是明智的。看起来key 应该有自己的索引。哈希索引就足够了,因为key 将始终进行相等比较。 namecontent(按此顺序)可以放入跳过列表索引中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2022-07-26
      • 1970-01-01
      • 2020-01-21
      相关资源
      最近更新 更多