Couchbase N1QL join query

荒凉一梦 提交于 2019-12-19 04:23:30

问题


need help with a query in Couchbase enviroment.

Both document 1 and document 2 is in the same bucket.

First the query will get the document message1 using this query:

SELECT uid, message, sent_by FROM bucket USE KEYS "message1"

Second, it need to get username from document 2. How can i create a join statement that will fetch username from a given document name, using the query above?

Overview

Document 1 document name = message1

[
  {
    "uid": "1",
    "message": "hello",
    "sent_by": "username"
  }
]

Document 2 document name = user1

[
  {
    "username": "username"
  }
]

回答1:


Assuming that the name of Document2 is based on the uid of Document1, that is, "user1" is based on uid=1, you can do the following:

SELECT d1.uid, d1.message, d1.sent_by, d2.username
FROM mybucket d1 USE KEYS "message1"
JOIN mybucket d2 ON KEYS "user" || d1.uid;


来源:https://stackoverflow.com/questions/37373860/couchbase-n1ql-join-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!