将查询返回的对象嵌套为大小为1的数组

时间:2019-06-17 19:46:01

标签: couchbase n1ql

我目前正在尝试从数组中查询特定对象。但是,查询结果返回的是单个对象,而不是大小为1的数组(其中一个对象填充了该数组)。

我的查询:

SELECT meta(bn).id as _ID, meta(bn).cas as _CAS, bn.name
    FIRST t FOR t IN properties WHEN t.id = "1111" END as property
FROM bucket-name as bn
WHERE ANY t IN attributes SATISFIES t.id = "1111" END

结果:

[
{
    "_CAS": 0000,
    "_ID": "1111",
    "name": "my name",
    "property": {
        "id": "1111",
        "name": "my property name"
    }
}
]

我宁愿得到的是。几乎完全一样,只是“属性”现在是一个数组

[
{
    "_CAS": 0000,
    "_ID": "1111",
    "name": "my name",
    "property": [
        {
            "id": "1111",
            "name": "my property name"
        }
    ]
}
]

1 个答案:

答案 0 :(得分:2)

首先更改为阵列https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/collectionops.html 使用FIRST时,循环在第一个元素满足时停止并返回该元素。 使用ARRAY时,循环将一直持续到结束,并且所有合格元素都将作为ARRAY(即列表)返回

SELECT meta(bn).id as _ID, meta(bn).cas as _CAS, bn.name
    ARRAY t FOR t IN properties WHEN t.id = "1111" END AS property
FROM `bucket-name` as bn
WHERE ANY t IN attributes SATISFIES t.id = "1111" END;