Elder Trevisan
Elder Trevisan

Reputation: 21

Match an attribute in an array on a vertex in ArangoDb using Python

I have the document of vertex[a] which has a border with the vertex of document[b], and I wonder how to traverse the array of vertex[a]:

[a]: {"name": "Tom", "age": 30, "colors": ["green", "blue", "red"]}
[b}: {"city", "NY"}

I'm trying as follows:

mydb.execute_query cursor = (
    """
    FOR i IN GRAPH_NEIGHBORS (
        'my_graph', 'citys/ny',
        {neighborExamples: {colors: 'green'}}
        ) RETURN i
    """
    )
    for doc in cursor:
        print (doc)

But does not return anything! If I try another attribute other than an array it works normally.

Upvotes: 1

Views: 70

Answers (2)

Elder Trevisan
Elder Trevisan

Reputation: 21

Thank mchacki and dothebarth, you made me realize that besides showing me the way how i do the search, also indirectly showed me that I was using the wrong function. I did the code below and ran perfectly. Thank you!

cursor = mydb.execute_query(
    """
    FOR i IN GRAPH_VERTICES(
        'my_graph',
        {city:'NY'},
        {}
        )
        FILTER 'green' IN i.colors
        RETURN i.name
    """
    )
for doc in cursor:
    print(doc)

>>"Tom"

Upvotes: 1

mchacki
mchacki

Reputation: 3267

right now GRAPH_NEIGHBORS() only support equality comparison of Examples. What you are querying for is "green" IN colors. This can right now only be expressed by post-filtering. Like this:

mydb.execute_query cursor = ( """
  FOR i IN GRAPH_NEIGHBORS ('my_graph', 'citys/ny', {}) 
    FILTER 'green' IN i.colors RETURN i
    """ ) doc is in cursor:
  print (doc)

should give you the desired result.

Upvotes: 2

Related Questions