使用n1ql couchbase在json数组列表之间进行双连接

时间:2016-08-19 14:40:31

标签: json join couchbase n1ql nosql

我有以下名为User的json条目。有一个 selectedItemList 列表可以让项目对用户负责。

"myproject": {
  "_class": "com.demo.model.entity.User",
  "active": true,
  "createDate": 1470826033000,
  "description": "",
  "firstname": "Tom",
  "selectedItemList": [
    "MTR44",
    "MTR55",
    "MTR66"
  ],
  "type": "user",
  "username": "tom"
}

以下3项将itemId保持为唯一值

    "myproject": {
  "_class": "com.demo.model.entity.Item",
  "deleted": false,
  "factor": 0,
  "itemId": "MTR66",
  "type": "item"
}

"myproject": {
  "_class": "com.demo.model.entity.Item",
  "deleted": false,
  "factor": 0,
  "itemId": "MTR55",
  "type": "item"
}

"myproject": {
  "_class": "com.demo.model.entity.Item",
  "deleted": false,
  "factor": 0,
  "itemId": "MTR44",
  "type": "item"
}

以下命令将项目ID保存为列表

"myproject": {
    "_class": "com.demo.model.entity.Orders",
    "model": "DR-503",
    "deleted": false,
    "itemIdList": [
        "MTR66",
        "MTR55"
    ],
    "type": "order"
}

"myproject": {
    "_class": "com.demo.model.entity.Orders",
    "model": "DR-504",
    "deleted": false,
    "itemIdList": [
        "MTR44"
    ],
    "type": "order"
}

我的问题是:我尝试获取负责用户“tom”下的项目的订单列表。 我的方式是:从加入的订单中获取商品。但我无法与用户tom的selectedItemList和itemId一起加入(因为它不是一个objest,它是一个json数组)。

SELECT META(myproject).id as _ID, META(myproject).cas as _CAS, myproject.*  
FROM myproject
LEFT JOIN myproject item ON KEYS ARRAY itemId FOR itemId IN myproject.itemIdList 
LEFT JOIN ........

所以,我在第二次加入后无法写,我对此一无所知。

1 个答案:

答案 0 :(得分:0)

试试这个:

create index idxorderlist on myproject(itemIdList);

SELECT *
FROM  myproject u USE KEYS ["tom"]
      LEFT OUTER JOIN myproject item ON KEYS u.selectedItemList
      LEFT OUTER JOIN myproject `order` ON KEY `order`.itemIdList FOR item
;