I have two buckets that needs to be joined. For one of them document keys look like
part1/part2/part3 and the second bucket records have field matching to the part3. How can I run joins using part of the document key?
select * from bucket2
join bucket1 on keys bucket1.part3
How should I modify my query to succeed ? Is it possible to do so?
Can you post 4 things: document1, document2, the key of document1, the key of document2.
So I want to join using document2.transaction-id which is the part of the key in the document1.
Here you go.
FROM bucket2 USE KEYS "23245"
JOIN bucket1 ON KEYS "MTPC/2016-11-18/" || bucket2.`transaction-id`
The problem with this solution the my prefix can change. It can be “MTPC/2016-11-18/…” or “ABCD/2014-11-12/”…
That part can be an expression. It does not have to be a constant.
The problem is that I don’t know how to put a expression. Because prefix can have any value following ‘/’ separated pattern. maybe there is a way to put a regex instead of
JOIN bucket1 ON KEYS “MTPC/2016-11-18/” || bucket2.
You can use any expression.
JOIN bucket1 ON KEYS _some_expression_
some_expression can be any N1QL expression. See the documentation on all functions and expressions.
I followed docs
As I understand I can use LIKE operator expression to make a join with key? I just don’t know how to put that like into the right place to make it work.
The following doesn’t work
on KEYS like ‘%’ || (TO_STRING(av.
I also want to try to use STRING functions like POSITION and SUBSTR to get the part of the string that I want. But I don’t know how to apply it to the document key not the field.
Use the function META( mybucket ).id to access the document key.
Thanks, but it is still not clear how should I use it in join.
ON KEYS statement uses a field not key. I would appreciate any simple example.
ON KEYS uses any expression. Please use the N1QL language reference, not any other documentation:
Give me some specifics, and I can give you another example.
Where keys are marked as bold. We are interested only in document1’s keys. document2.transaction-id field matches part of the key of the first document. Though I understand that I can apply an expression but in the JOIN statement we use document2.transaction_id and I don’t know how to apply expression on document1’s key itself.
I would appreciate an example.
Thanks for your time.
Is the document1 key prefix a wildcard? That is, given a document2, there is no way to know the prefix of the document1 key?
That’s right. The prefix is unknown.
Then it is not possible to perform the JOIN in this direction, from document2 to document1.
You need to use 4.5.1 EE, and then join from document1 to document2, using an index JOIN.
We made a change in our bucket structure to make it work. Thanks