问题描述
需要在Couchbase环境中进行查询的帮助。
need help with a query in Couchbase enviroment.
文档1和文档2都位于同一存储桶中。
Both document 1 and document 2 is in the same bucket.
首先,查询将使用以下查询获取文档message1:
First the query will get the document message1 using this query:
SELECT uid, message, sent_by FROM bucket USE KEYS "message1"
第二,它需要从文档2中获取用户名。如何创建一个join语句,该语句将从给定的用户名中获取用户名文档名称,使用上面的查询?
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?
文档1
文档名称= message1
Document 1document name = message1
[
{
"uid": "1",
"message": "hello",
"sent_by": "username"
}
]
文档2
文档名称= user1
Document 2document name = user1
[
{
"username": "username"
}
]
推荐答案
假设假设Document2的名称基于Document1的uid,即 user1基于uid = 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;
这篇关于Couchbase N1QL连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!