Couchbase-从对象数组中选择字段的子集 [英] Couchbase - SELECT a subset of fields from array of objects
问题描述
我正在使用 travel-sample
数据集,并正在运行以下查询:
I am using the travel-sample
data set, and am running the following query:
SELECT id, schedule FROM `travel-sample`WHERE type = "route" LIMIT 1;
它返回以下结果:
[
{
"id": 10000,
"schedule": [
{
"day": 0,
"flight": "AF198",
"utc": "10:13:00"
},
{
"day": 0,
"flight": "AF547",
"utc": "19:14:00"
},
...
]
}
]
但是,我不想返回 schedule.$.day
字段;即我希望我的结果是:
However, I don't want to return the schedule.$.day
field; i.e. I want my results to be:
[
{
"id": 10000,
"schedule": [
{
"flight": "AF198",
"utc": "10:13:00"
},
{
"flight": "AF547",
"utc": "19:14:00"
},
...
]
}
]
如何仅从对象数组中选择对象字段的子集?
How can I SELECT
only a subset of object fields from an array of objects?
我尝试了 UNNEST
,但是我不想为每个 schedule
元素单独记录-我希望将 schedule
元素保持嵌套在文档中.
I have tried UNNEST
but I don't want to have a separate record for each schedule
element - I want the schedule
elements to remain nested inside the document.
我也尝试过使用 OBJECT_REMOVE
SELECT id, ARRAY OBJECT_REMOVE(x, 'day') FOR x in schedule END AS schedule FROM `travel-sample` WHERE type = "route" LIMIT 1;
但是我想将字段列入白名单而不是黑名单.
But I want to whitelist rather than blacklist fields.
推荐答案
您上一次尝试已结束.不用使用 OBJECT_REMOVE
,您只需构造要返回的对象即可.
Your last attempt was close. Instead of using OBJECT_REMOVE
, you can simply construct the object you want returned.
SELECT id, ARRAY {"flight": x.flight, "utc": x.utc} FOR x in schedule END AS schedule FROM `travel-sample` WHERE type = "route" LIMIT 1;
您将获得以下结果:
[
{
"id": 10000,
"schedule": [
{
"flight": "AF198",
"utc": "10:13:00"
},
{
"flight": "AF547",
"utc": "19:14:00"
},
...
]
}
]
这篇关于Couchbase-从对象数组中选择字段的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!