首页 > 解决方案 > ARANGODB AQL MATCHES() with nested objects not possible

问题描述

I use MATCHES() AQL function to search for entries in the arango database matching an example. This feature works nice for plain examples, but I cannot get it work properly with the nested features. See example:

RETURN MATCHES(
{ "a" : { "c" : 1 }, "b" : 1 },
{ "a" : { "c" : 1 } },
false
)

This returns true, however if I try:

RETURN MATCHES(
{ "a" : { "c" : 1, "b" : 1 }},
{ "a" : { "c" : 1 } },
false
)

It returns false !! (I expected to return true)

I have read that this is known in the Query by example section https://www.arangodb.com/docs/stable/data-modeling-documents-document-methods.html#query-by-example

Their solution is to use dot notation, but it does not work in AQL

Following their example:

RETURN MATCHES(
{ "a" : { "c" : 1, "b" : 1 } },
{ "a.c" : 1 },
false
)

returns false (and I would expect to return true)

How can I then, use the MATCHES() for nested attributes?

FYI: I use arangodb v3.5.5-1

Clarification: I want to get a match of { "a" : { "c" : 1, "b" : 1 } } by giving { "a" : { "c" : 1 } } as example

I've posted the Issue in ArangoDB repository: https://github.com/arangodb/arangodb/issues/12541

标签: nestedmatcharangodbaql

解决方案


MATCHES比较属性 - 它不关心属性的类型,因此如果您尝试匹配嵌套对象,它们必须具有相同的属性/值;因此,您不能将任意深度的结构覆盖并检查其对应关系。

在给定的示例中,您可以选择子结构LET并使用MATCHES

LET doc = { "a" : { "c" : 1, "b" : 1 }}
LET a = doc.a

RETURN MATCHES(
  a,
  { "c": 1}
)

要利用 arangojs 功能使用路径来窥视结构,您可以编写一个使用示例查询的用户函数并从 AQL 调用它(https://www.arangodb.com/docs/stable/aql/extending.html)。

注意事项:您的语言的 arangodb 客户端库应该提供对注册用户函数的方便访问(例如class AqlUserFunctionsin arangodb-php)。


推荐阅读