Search code examples
pythonarangodbaql

How to return a list of arangodb documents that match an item in a list?


Using python and AQL I am trying to return a list of vertices that match any item in a given list. The current result from the db I am getting is an empty list.

The python equivalent would be this:

list_of_terms = ["yellow", "blue"]
list_of_vertices = ["yellow", "green"]

terms = [term for term in list_of_terms if term in list_of_vertices]

print(terms)

An example of one AQL query I tried.

For doc in some_collection
    FILTER doc.name==@list_of_terms
    RETURN doc

And the full full function using python-arango

bind_vars = {
    "lookup_terms": list_of_terms
   } 

Thanks in advance

qry = "FOR doc IN `{0}` FILTER doc.name== @lookup_terms AND doc.text != null RETURN doc".format(collection_nm)
print(qry)
cursor = db.aql.execute(
    qry,
    bind_vars=bind_vars,
    batch_size=10,
    count=True
    )

Solution

  • You should use the IN operator:

    FOR doc IN some_collection
        FILTER doc.name IN @list_of_terms
        RETURN doc
    

    From the documentation:

    IN: test if a value is contained in an array

    See https://www.arangodb.com/docs/stable/aql/operators.html#range-operator

    Your python code would then become:

    bind_vars = {
        "lookup_terms": list_of_terms
    } 
    qry = "FOR doc IN `{0}` FILTER doc.name IN @lookup_terms AND doc.text != null RETURN doc".format(collection_nm)
    print(qry)
    cursor = db.aql.execute(
        qry,
        bind_vars=bind_vars,
        batch_size=10,
        count=True
    )