简体   繁体   English

如何使用ArangoDB迭代嵌套的JSON?

[英]How to iterate nested JSON with ArangoDB?

I have JSON with next structure: [[{"QID":1,"AID":1},{"SubAID":[]}],[{"QID":2,"AID":1},{"SubAID":[2,4]}],[{"QID":3,"AID":1},{"SubAID":[]}],{"MaxArea":"90","MinArea":"16"}] 我的JSON具有以下结构: [[{"QID":1,"AID":1},{"SubAID":[]}],[{"QID":2,"AID":1},{"SubAID":[2,4]}],[{"QID":3,"AID":1},{"SubAID":[]}],{"MaxArea":"90","MinArea":"16"}]

Is this ok for ArangoDB? ArangoDB可以吗? I tried to push it's with http-api (I don't have a driver for my language - D), but I got error 500 , maybe I'm wrong, maybe this JSON is not correct for ArangoDB. 我尝试将其与http-api一起使用(我没有针对我的语言的驱动程序-D),但出现error 500 ,也许我错了,也许此JSON对ArangoDB不正确。

Also I would like to get any examples of iterating such JSON with AQL. 我也想获得使用AQL迭代此类JSON的任何示例。 For example if I need value of SubAID where QID is 2 , how should I write such query? 例如,如果我需要QID2SubAID值,我应该如何编写这样的查询?

On the main level, documents in ArangoDB are JSON objects. 在主要级别上,ArangoDB中的文档是JSON对象。 The JSON you're showing above in a JSON array, so it won't be accepted as is as a document. 您上面在JSON数组中显示的JSON,因此不会作为文档原样接受。

What you can do is wrap the above JSON in an object attribute, for example data : 您可以做的是将上述JSON包装在object属性中,例如data

{"data":[[{"QID":1,"AID":1},{"SubAID":[]}],[{"QID":2,"AID":1},{"SubAID":[2,4]}],[{"QID":3,"AID":1},{"SubAID":[]}],{"MaxArea":"90","MinArea":"16"}]}

Regarding querying the data: it looks like data is an array containing arrays and an object. 关于查询数据:看起来数据是一个包含数组和对象的数组。 Inside the arrays there is an object with attributes QID and AID at array position 0, and an object containing SubAid at array position 1. 在数组内部,在数组位置0处有一个具有QIDAID属性的对象,在数组位置1处有一个包含SubAid的对象。

If that's true for all data, the a query like as follows should find the documents that a QID value of 2 : 如果对所有数据都是如此,则如下查询应找到QID值为2的文档:

/* iterate over all documents in collection */
FOR doc IN collection 

  LET s = (
    /* iterate over embedded attribute "data */
    FOR sub IN doc.data 

      /* look at QID at array position 0 */
      FILTER sub[0].QID == 2 

      /* first match is sufficient */
      LIMIT 1 

      /* return SubAID value from array position 1 */
      RETURN sub[1].SubAID 
  ) 

  /* only return documents with a match */
  FILTER LENGTH(s) > 0 

  /* return first result from subquery (subquery result is always an array) */
  RETURN s[0] 

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

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