首页 > 解决方案 > 如何返回与列表中的项目匹配的 arangodb 文档列表?

问题描述

使用 python 和 AQL 我试图返回与给定列表中的任何项目匹配的顶点列表。我得到的数据库的当前结果是一个空列表。

python等价物是这样的:

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)

我尝试过的一个 AQL 查询示例。

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

和完整的功能使用python-arango

bind_vars = {
    "lookup_terms": list_of_terms
   } 

提前致谢

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
    )

标签: pythonarangodbaql

解决方案


您应该使用IN运算符:

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

从文档中:

IN: 测试一个值是否包含在数组中

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

您的 python 代码将变为:

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
)

推荐阅读