Couchbase - 从对象数组中选择字段的子集

时间:2018-06-18 11:31:46

标签: couchbase

我正在使用travel-sample数据集,并且正在运行以下查询:

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字段;即我希望我的结果是:

[
  {
    "id": 10000,
    "schedule": [
      {
        "flight": "AF198",
        "utc": "10:13:00"
      },
      {
        "flight": "AF547",
        "utc": "19:14:00"
      },
      ...
    ]
  }
]

我怎样SELECT只能从一组对象中获取对象字段的子集?

我尝试了UNNEST,但我不想为每个schedule元素分别创建一个记录 - 我希望schedule元素保持嵌套在文档中。

我也尝试过使用OBJECT_REMOVE

SELECT id, ARRAY OBJECT_REMOVE(x, 'day') FOR x in schedule END AS schedule FROM `travel-sample` WHERE type = "route" LIMIT 1;

但我想将白名单列入白名单而不是黑名单。

1 个答案:

答案 0 :(得分:1)

你的最后一次尝试已经结束了。您可以简单地构造要返回的对象,而不是使用OBJECT_REMOVE

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"
      },
      ...
    ]
  }
]