简体   繁体   English

使用AQL迭代查询ArangoDB

[英]Iterative querying the ArangoDB using AQL

I have stored the JSON data in ArangoDB collection in the following format. 我已经按照以下格式将JSON数据存储在ArangoDB集合中。

{ 
  "data": {
    "1":   [ {"db_name": "DSP"}, {"rel": "2"} ], 
    "2":   [ {"rel_name": "DataSource"}, {"tuple": "201"}, {"tuple": "202"}, {"tuple": "203"} ],
    "201": [ {"src_id": "Pos201510070"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151007"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "postgres"}, {"port": "None"} ],
    "202": [ {"src_id": "pos201510060"}, {"src_name": "Postgres"},{"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "DSP"}, {"port": "5432"} ],
    "203": [ {"src_id": "pos201510060"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"},{"src_type": "Structured"},{"db_name": "maindb"},{"port": "5432"} ]
  }
}

I am new to ArangoDB. 我是ArangoDB的新手。 I have no idea about the storing and querying the data from ArangoDB. 我不知道如何从ArangoDB存储和查询数据。 In my data there is no any predefined key, and the data get populated with time. 在我的数据中,没有任何预定义键,并且数据随时间填充。 My data is just like a semi-structured data which do not have any fixed number of attributes, and little bit complex due to its iterative list structure. 我的数据就像半结构化数据,没有任何固定数量的属性,并且由于其迭代列表结构而有点复杂。

First, anyone can suggest me the best way for storing the above format in ArangoDB. 首先,任何人都可以建议我将上述格式存储在ArangoDB中的最佳方法。

Second, I want to query this data in following manner: by specifying any key (not known in advance, by specifying it at runtime), or by specify combinations of key and value pair, eg, Key1 == value1 , or the combination using AND or OR logical operators like Key1 == value1 AND Key2 == value2 OR Key3== value3 . 其次,我想以以下方式查询此数据:通过指定任何键(预先未知,在运行时指定它),或指定键和值对的组合,例如Key1 == value1 ,或使用AND或OR逻辑运算符,例如Key1 == value1 AND Key2 == value2 OR Key3== value3

So, how we can iterate over the above data? 那么,我们如何迭代以上数据呢?

If you really want to store data in a structure like this, without any predefined attribute names, you can still iterate over the data by converting it into a normalized structure on-the-fly. 如果您确实想在没有任何预定义属性名称的情况下将数据存储在这样的结构中,则仍可以通过将其即时转换为标准化结构来遍历数据。

The following AQL query creates a flat list of key/value pairs for each document: 以下AQL查询为每个文档创建键/值对的平面列表:

FOR doc IN collection 
  LET attributes = ATTRIBUTES(doc.data) 
  FOR attribute IN attributes 
    FOR arrayItem IN doc.data[attribute] 
      LET key = ATTRIBUTES(arrayItem)[0] 
      LET value = arrayItem[key] 
      RETURN { _key: doc._key key: key, value: value }

The result of this query will be something like this: 该查询的结果将是这样的:

[ 
  { 
    "_key" : "864582648369", 
    "key" : "password", 
    "value" : "root" 
  }, 
  { 
    "_key" : "864582648369", 
    "key" : "db_name", 
    "value" : "postgres" 
  }, 
  { 
    "_key" : "864582648369", 
    "key" : "port", 
    "value" : "None" 
  }, 
  ...
]

Now you can apply the filtering easily by adding the filter conditions of choice: 现在,您可以通过添加选择的过滤条件来轻松应用过滤:

FOR doc IN collection 
  LET attributes = ATTRIBUTES(doc.data) 
  FOR attribute IN attributes 
    FOR arrayItem IN doc.data[attribute] 
      LET key = ATTRIBUTES(arrayItem)[0] 
      LET value = arrayItem[key] 
      FILTER key == 'password' || key == 'port' || (key == 'db_name' && value == 'postgres') 
      RETURN { _key: doc._key, key: key, value: value }

Note that when the data structure changes (more/less levels of nesting), the above won't work anymore. 请注意,当数据结构更改时(更多/更少的嵌套级别),以上内容将不再起作用。 The query assumes documents with a structure as presented in the question. 该查询假定文档具有问题中呈现的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM