Hi,
We are working on the Couchbase connector in StrongLoop application.
My work is to translate the StrongLoop syntax into N1QL syntax and submit the N1QL statement to Couchbase server. I have couple of questions regarding how to use N1QL “JOIN” statement.
Here is my scenario:
there are three types of documents, “TicketUser”, “UserAddress” and “Ticket”. Each TicketUser could have multiple UserAddress, and also have multiple Ticket.
The testing data has two TicketUser documents:
ticket_user_0000001
{
“doctype”: “TicketUser”,
“name”: “Eric Chou”,
“occupation”: “Software Engineer”
}
ticket_user_0000002
{
“doctype”: “TicketUser”,
“name”: “Nolan Dubeau”,
“occupation”: “Lead Software Developer”
}
And there are three UserAddress documents: two UserAddress is for the TicketUser “ticket_user_0000001”, and one UserAddress is for the TicketUser “ticket_user_0000002”
user_address_0000001
{
“doctype”: “UserAddress”,
“address”: “1234 Maud street”,
“city”: “Toronto”,
“user_id”: “ticket_user_0000002”
}
user_address_0000002
{
“doctype”: “UserAddress”,
“address”: “91 Crayford Drive”,
“city”: “Scarborough”,
“user_id”: “ticket_user_0000001”
}
user_address_0000003
{
“doctype”: “UserAddress”,
“address”: “125 Bamburough Circle”,
“city”: “Scarborough”,
“user_id”: “ticket_user_0000001”
}
You can see that the “user_id” field in the UserAddress documents is the foreign key to the TicketUser.
Then, we also have 5 Ticket documents, two of them belong to “ticket_user_0000001” and three of them belong to “ticket_user_0000002”
ticket_0000001
{
“doctype”: “Ticket”,
“style”: “Alert”,
“description”: “Send panic alert to all the others”,
“user_id”: “ticket_user_0000001”
}
ticket_0000002
{
“doctype”: “Ticket”,
“style”: “Regular”,
“description”: “Send regular messages to the friends”,
“user_id”: “ticket_user_0000001”
}
ticket_0000003
{
“doctype”: “Ticket”,
“style”: “Regular”,
“description”: “Send regular messages to the friends”,
“user_id”: “ticket_user_0000002”
}
ticket_0000004
{
“doctype”: “Ticket”,
“style”: “Alert”,
“description”: “Send panic alert to all the others”,
“user_id”: “ticket_user_0000002”
}
ticket_0000005
{
“doctype”: “Ticket”,
“style”: “Alert”,
“description”: “Send panic alert to all the others”,
“user_id”: “ticket_user_0000002”
}
Now, as a baby step try, I try to obtain all the TicketUser along with their UserAddress documents, the N1QL statement is the following:
SELECT * FROM default useraddress JOIN default ticketuser KEYS [useraddress.user_id] WHERE useraddress.doctype=‘UserAddress’ AND ticketuser.doctype=‘TicketUser’
This statement works as I expected: three records were returned.
So, question one: I found that if I swap the two sides of the JOIN, I will get nothing returned, look at this statement:
SELECT * FROM default ticketuser JOIN default useraddress KEYS [useraddress.user_id] WHERE useraddress.doctype=‘UserAddress’ AND ticketuser.doctype=‘TicketUser’
--looks like I have to keep the "useraddress" to be the left side of JOIN. Is that true?
And then, the second question:
I want to collect not only TicketUser and UserAddress documents, but also Ticket documents, I wrote the statement like this but got nothing:
SELECT
- FROM default useraddress JOIN default ticketuser KEYS
[useraddress.user_id] LEFT JOIN default ticket KEYS [ticket.user_id]
WHERE useraddress.doctype=‘UserAddress’ AND ticketuser.doctype=‘TicketUser’ AND ticket.docType=‘Ticket’
–could you let me know how to write the correct syntax for the two JOINs in the same statement?
Thanks in advance