12.匹配的元素与任何嵌套的数组

  • ANY -  数组节点中的其中一个子节点满足条件即可。

  • EVERY - 数组节点中的任何一个子节点都需要满足条件。

有时你想过滤基于数组嵌套内的文档。在本教程中数据集,一些文档包含一个数组的children,每个child都有一个名称和一个年龄。

在下边的例子,我们想找到一些人并且其child年龄其中一个满足10岁以上的即可。

ANY Query:

SELECT fname, children
    FROM tutorial 
        WHERE ANY child IN tutorial.children SATISFIES child.age > 10  END

结果:

{
  "results": [
    {
      "children": [
        {
          "age": 17,
          "fname": "Aiden",
          "gender": "m"
        },
        {
          "age": 2,
          "fname": "Bill",
          "gender": "f"
        }
      ],
      "fname": "Dave"
    },
    {
      "children": [
        {
          "age": 17,
          "fname": "Xena",
          "gender": "f"
        },
        {
          "age": 2,
          "fname": "Yuri",
          "gender": "m"
        }
      ],
      "fname": "Earl"
    },
    {
      "children": [
        {
          "age": 17,
          "fname": "Abama",
          "gender": "m"
        },
        {
          "age": 21,
          "fname": "Bebama",
          "gender": "m"
        }
      ],
      "fname": "Ian"
    }
  ]
}

在下边的例子,我们想找到一些人并且其child年龄都必须满足10岁以上的即可。

EVERY Query:

SELECT fname, children
FROM tutorial
WHERE EVERY child IN tutorial.children SATISFIES child.age > 10 END

结果:

{
  "results": [
    {
      "children": [
        {
          "age": 17,
          "fname": "Abama",
          "gender": "m"
        },
        {
          "age": 21,
          "fname": "Bebama",
          "gender": "m"
        }
      ],
      "fname": "Ian"
    }
  ]
}

Last updated

Was this helpful?