简体   繁体   English

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

[英]Arango DB Filter query for print array of value

Given the following document structure:给定以下文档结构:

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

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

I have telecom and name array.我有电信和名称数组。 I want to query to compare if name[*].use=='official' then print corresponding give array.我想查询比较 name[*].use=='official' 然后打印相应的给定数组。

Expected result: "given": [ "Peter", "James" ]预期结果: “给定”:[“彼得”,“詹姆斯”]

client.name[*].use is an array, so you need to use an array operator. client.name[*].use是一个数组,所以需要使用数组运算符。 It can be either of the following:它可以是以下任何一种:

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

To return just the given names from the 'official' array, you can use a subquery:要从“官方”数组中返回给定的名称,您可以使用子查询:

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

Alternatively, you can use an 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])
  }

Result:结果:

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

In your original post, the example document and query didn't match, but assuming the following structure:在您的原始帖子中,示例文档和查询不匹配,但假设结构如下:

{
  "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"
      ]
    }
  ]
}

… here is a possible query: …这是一个可能的查询:

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]
  }

Note that client.telecom[*].value LIKE "..." causes the array of phone numbers to be cast to a string "[\"+1 (03) 5555 6473 82\",\"+1 (252) 5555 910 920 3\"]" against which the LIKE operation is run - this kind of works, but it's not ideal.请注意, client.telecom[*].value LIKE "..."导致电话号码数组被转换为字符串"[\"+1 (03) 5555 6473 82\",\"+1 (252) 5555 910 920 3\"]"运行 LIKE 操作 - 这种工作,但并不理想。

CONTAINS() is also faster than LIKE with % wildcards on both sides. CONTAINS()也比两边都带有%通配符的LIKE快。

It would be possible that there are multiple 'official' elements, which might require an extra level of array nesting.可能有多个“官方”元素,这可能需要额外级别的数组嵌套。 Above query produces:上面的查询产生:

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

If you know that there is only one element or restrict it to one element explicitly then you can get rid of one of the wrapping square brackets with FIRST() or FLATTEN() .如果您知道只有一个元素或将其明确限制为一个元素,那么您可以使用FIRST()FLATTEN()摆脱其中一个环绕方括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM