27.UNNEST

UNNEST运算允许您将嵌套的内容提升到与他们的父对象级别,加入他们的父对象。

在tutorial数据库有每个人都有一个children数组。如果我们有三个人,每个都有两个孩子,我们会得到6的结果,每个都包含1人,1的孩子。

右边的查询连接Dave和他的两个孩子。

Query:

SELECT * 
    FROM tutorial AS parent
        UNNEST parent.children
            WHERE parent.fname = 'Dave'

结果:

{
  "results": [
    {
      "children": {
        "age": 17,
        "fname": "Aiden",
        "gender": "m"
      },
      "parent": {
        "age": 46,
        "children": [
          {
            "age": 17,
            "fname": "Aiden",
            "gender": "m"
          },
          {
            "age": 2,
            "fname": "Bill",
            "gender": "f"
          }
        ],
        "email": "dave@gmail.com",
        "fname": "Dave",
        "hobbies": [
          "golf",
          "surfing"
        ],
        "lname": "Smith",
        "relation": "friend",
        "title": "Mr.",
        "type": "contact"
      }
    },
    {
      "children": {
        "age": 2,
        "fname": "Bill",
        "gender": "f"
      },
      "parent": {
        "age": 46,
        "children": [
          {
            "age": 17,
            "fname": "Aiden",
            "gender": "m"
          },
          {
            "age": 2,
            "fname": "Bill",
            "gender": "f"
          }
        ],
        "email": "dave@gmail.com",
        "fname": "Dave",
        "hobbies": [
          "golf",
          "surfing"
        ],
        "lname": "Smith",
        "relation": "friend",
        "title": "Mr.",
        "type": "contact"
      }
    }
  ]
}

Last updated

Was this helpful?