如何在couchbase中访问Jobject中的json数组? (SELECT查询)

时间:2017-09-11 03:51:39

标签: couchbase nosql

这是我的Json。我有props.bom_concrete_type.id,我需要得到“desc”需要什么样的查询?帮帮我

[
  {
    "create_date": "2017-06-07T09:35:12.2391092+08:00",
    "id": "optional_dictionary",
    "model": "optional_dictionary",
    "props": {
      "bom_concrete_type": [
        {
          "desc": "Shotcrete",
          "id": "1",
          "shortname": null
        },
        {
          "desc": "Concrete",
          "id": "2",
          "shortname": null
        }
      ],
      "bom_production_type": [
        {
          "desc": "Underground",
          "id": "1",
          "shortname": null
        },
        {
          "desc": "Surface",
          "id": "2",
          "shortname": null
        },
        {
          "desc": "TKAJV",
          "id": "3",
          "shortname": null
        }
      ],
      "mixorder_comment": [
        {
          "desc": "No order",
          "id": "1",
          "shortname": null
        },
        {
          "desc": "Client canceled",
          "id": "2",
          "shortname": null
        },
        {
          "desc": "Batch plant canceled",
          "id": "3",
          "shortname": null
        },
        {
          "desc": "Re-schedule/Requester cancelled",
          "id": "4",
          "shortname": null
        },
        {
          "desc": "Batch plant shutdown",
          "id": "5",
          "shortname": null
        },
        {
          "desc": "Client shutdown",
          "id": "6",
          "shortname": null
        },
        {
          "desc": "Weather Condition",
          "id": "7",
          "shortname": null
        },
        {
          "desc": "Client equipment",
          "id": "8",
          "shortname": null
        },
        {
          "desc": "Batch plant equipment",
          "id": "9",
          "shortname": null
        },
        {
          "desc": "Incident/Accident",
          "id": "10",
          "shortname": null
        },
        {
          "desc": "HSE Issue",
          "id": "11",
          "shortname": null
        },
        {
          "desc": "Client slow production",
          "id": "12",
          "shortname": null
        },
        {
          "desc": "Time conflict between requests",
          "id": "13",
          "shortname": null
        },
        {
          "desc": "Normal operation",
          "id": "14",
          "shortname": null
        },
        {
          "desc": "Slickline or underground maintenance by Client(Mining)",
          "id": "15",
          "shortname": null
        }
      ]
    },
    "version": null
  }
]

1 个答案:

答案 0 :(得分:0)

对于这种类型的查询,您需要使用其中一个数组运算符(https://developer.couchbase.com/documentation/server/4.6/n1ql/n1ql-language-reference/collectionops.html)。

我在上面的文档中输入了一个名为“default”的存储桶。此查询将为您提供'desc'字段(需要在后面标记中引用,因为它是保留字,“按...排序”)。

select first e.`desc` 
    for e in props.bom_concrete_type when e.id = "1" end as `desc` 
from default;

查询结果为:

[
  {
    "desc": "Shotcrete"
  }
]

如果您只想要裸值,可以使用此查询:

select raw first e.`desc` 
  for e in props.bom_concrete_type when e.id = "1" end 
from default;

结果是:

[
  "Shotcrete"
]