I am migrating from an existing system where I had all data sitting in Java objects in memory (maps with direct keys). I have run into a little challenge as to how to change some of my code to use N1QL.
I am using Community server 5.1.1 and have all data in a data
bucket.
I have a User
that can have several FishingTrip
docs (and there is a userkey on the trip) and each trip can have zero to many Catch
docs (which actually also has a userkey). Each catch doc. references bait, species etc. that I have a key to do the direct lookup for.
I need to find the latest 10 catches for a particular user and show it with info from the trip as well as bait and species.
I have tried to start building a query like this:
SELECT t.*, c.*FROM data AS c JOIN data AS t
ON KEYS 'FishingTrip:' || c.fishingtripkey
WHERE c.type='Catch' AND t.type='FishingTrip' AND c.userkey='BA171123846CEBF1C1257CB2002DA330'
ORDER BY t.date DESC
LIMIT 10
I have also succeeded in getting the bait name with this query:
SELECT t.*, c.*,b.name as baitname FROM data AS c
JOIN data AS t ON KEYS 'FishingTrip:' || c.fishingtripkey
JOIN data AS b ON KEYS 'Bait:' || c.bait
WHERE c.type='Catch' AND t.type='FishingTrip' AND c.userkey='BA171123846CEBF1C1257CB2002DA330'
ORDER BY t.date DESC
LIMIT 10
However, if I try to add the species name and user name following the same approach they just don’t appear in the result.
SELECT t.*, c.*,b.name as baitname, u.name as username FROM data AS c
JOIN data AS t ON KEYS 'FishingTrip:' || c.fishingtripkey
JOIN data AS b ON KEYS 'Bait:' || c.bait
JOIN data AS u ON KEYS 'User:' || c.userkey
WHERE c.type='Catch' AND t.type='FishingTrip' AND c.userkey='BA171123846CEBF1C1257CB2002DA330'
ORDER BY t.date DESC
LIMIT 10
… and obviously we have not talked performance yet
I have a feeling that there must be an easier way to do what I am trying to obtain. Any input appreciated