On Jul 13, 2014, at 1:59 PM, Sebastien ARBOGAST <[email protected]>
wrote:
> With this I think I can filter my documents to get only open sessions, but
> how should I proceed to get only the one with the biggest start date for each
> owner in my query?
You probably want a view whose primary key is owner (because you want to query
for a specific owner) and secondary key is date (because you want to sort by
date.) The way you do that is by emitting a key that's an array of the form
@[owner, date]. That way you can query for all the rows with a specific owner,
by specifying a key range, and the rows will end up sorted by date. If you only
want the maximum date, then you just need to fetch the last row.
So in your query you set
descending = true // because you want to start with the max date
limit = 1 // because you only want the row with the max date
startKey = @[owner, @{}] // i.e. the last/max row with the given owner
endKey = @[owner] // i.e. the first row with the given owner
The @{} in the startKey is an idiom for "infinity", because by the sorting
rules a dictionary sorts after anything else. It's commonly used as a
placeholder when setting a range in a view that has array-based keys.
The docs for views and queries cover all of these concepts.
--Jens
--
You received this message because you are subscribed to the Google Groups
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mobile-couchbase/13D1BD6E-1373-48AA-AD26-E484BF676B49%40couchbase.com.
For more options, visit https://groups.google.com/d/optout.