用于打印值数组的 Arangodb 过滤器查询

Arango DB Filter query for print array of value

给定以下文档结构:

{
"name": [
{
  "use": "official",
  "family": "Chalmers",
  "given": [
    "Peter",
    "James"
  ]
},
{
  "use": "usual",
  "given": [
    "Jim"
  ]
},
{
  "use": "maiden",
  "family": "Windsor",
  "given": [
    "Peter",
    "James"
   ]
  }
 ]
}

查询: FOR client IN Patient FILTER client.name[*].use=='official' RETURN client.name[*].given

我有电信和姓名数组。 我想查询比较 if name[*].use=='official' 然后打印相应的 give array.

预期结果: “给出”:[ “彼得”, “詹姆士” ]

client.name[*].use是一个数组,所以需要用到数组运算符。它可以是以下之一:

  • 'string' in doc.attribute
  • doc.attribute ANY == 'string'
  • doc.attribute ANY IN ['string']

要return只是'official'数组中的给定名称,您可以使用子查询:

RETURN { given:
  FIRST(FOR name IN client.name FILTER name.use == 'official' LIMIT 1 RETURN name.given)
}

或者,您可以使用 inline expression:

FOR client IN Patient
  FILTER 'official' IN client.name[*].use
  RETURN { given:
    FIRST(client.name[* FILTER CURRENT.use == 'official' LIMIT 1 RETURN CURRENT.given])
  }

结果:

[
  {
    "given": [
      "Peter",
      "James"
    ]
  }
]

在您原来的 post 中,示例文档和查询不匹配,但假设结构如下:

{
  "telecom": [
    {
      "use": "official",
      "value": "+1 (03) 5555 6473 82"
    },
    {
      "use": "mobile",
      "value": "+1 (252) 5555 910 920 3"
    }
  ],
  "name": [
    {
      "use": "official",
      "family": "Chalmers",
      "given": [
        "Peter",
        "James"
      ]
    },
    {
      "use": "usual",
      "given": [
        "Jim"
      ]
    },
    {
      "use": "maiden",
      "family": "Windsor",
      "given": [
        "Peter",
        "James"
      ]
    }
  ]
}

…这是一个可能的查询:

FOR client IN Patient
  FILTER LENGTH(client.telecom[* FILTER
    CONTAINS(CURRENT.value, "(03) 5555 6473") AND
    CURRENT.use == 'official']
  )
  RETURN {
    given: client.name[* FILTER CURRENT.use == 'official' RETURN CURRENT.given]
  }

请注意,client.telecom[*].value LIKE "..." 导致 phone 数字数组被转换为字符串 "[\"+1 (03) 5555 6473 82\",\"+1 (252) 5555 910 920 3\"]",LIKE 操作针对该字符串 运行 - 这种工作方式,但这并不理想。

CONTAINS() 也比 LIKE 快,两边都是 % 通配符。

可能有多个 'official' 元素,这可能需要额外的数组嵌套级别。以上查询产生:

[
  {
    "given": [
      [
        "Peter",
        "James"
      ]
    ]
  }
]

如果您知道只有一个元素或将其明确限制为一个元素,那么您可以使用 FIRST()FLATTEN().[=28= 去掉其中一个方括号]