Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter on array value in ArangoDB

Tags:

arangodb

I have a document like this:

{   "baths": 2,   "beds": 3,   "id": "3225C",   "addrs": [
    {
      "line2": "",
      "line3": "",
      "state": "OH",
      "zip": "67845",
      "line1": "3225 ABC AVE",
      "city": "CLEVELAND"
    },
    {
      "line2": "",
      "line3": "",
      "state": "FL",
      "zip": "32818",
      "line1": "2438 DEF AVE",
      "city": "ORLANDO"
    }   ],   "homeAddress": {
    "line2": "",
    "line3": "",
    "state": "FL",
    "zip": "32818",
    "line1": "1234 CHICOTA AVE",
    "city": "ORLANDO"   },   "rentingAddresses": {
    "ownsObjects": true,
    "count": 0,
    "arrayManager": {},
    "items": []   },   "mailAddress": [
    "4561 RAYCO AVE",
    "",
    "",
    "ORLANDO",
    "FL",
    "32818"   ] }

I'm trying to find which clients have an addrs where the state is in "OH". My aql query is:

for client in clients.addrs
filter client.state == "OH"
return client

But I keep getting [1563] list expected. Is there some other way to work with arrays?

like image 746
Lerato Avatar asked Jun 22 '14 12:06

Lerato


2 Answers

you can do this:

FOR client IN clients
   FILTER 'OH' IN client.addrs[*].state
   RETURN client

this should return all clients which have at least one element in the "addrs" attribute with "state" == "OH"

like image 124
fceller Avatar answered Oct 03 '22 15:10

fceller


first you have to select a doc in that collection by using filter like this:

FOR doc IN clients
 FILTER doc.id == "3225C"
   FOR d IN doc.addrs
    FILTER d.state == "OH"
   RETURN d

this will return the addrs object in which state == "OH". if you want to get complete doc, just replace RETURN d with RETURN doc .

like image 39
Vinit Singh Avatar answered Oct 03 '22 15:10

Vinit Singh