0

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"}]

Is this ok for 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.

Also I would like to get any examples of iterating such JSON with AQL. For example if I need value of SubAID where QID is 2, how should I write such query?

stj
  • 9,037
  • 19
  • 33
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

1 Answers1

3

On the main level, documents in ArangoDB are JSON objects. The JSON you're showing above in a JSON array, so it won't be accepted as is as a document.

What you can do is wrap the above JSON in an object attribute, for example 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.

If that's true for all data, the a query like as follows should find the documents that a QID value of 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] 
stj
  • 9,037
  • 19
  • 33