I have a view with compound key like this [ product_id, since, shop_id], where:
“product_id” is a string(numbers only)
“since” is a long
“shop_id” is couchbase row id value of the other document.
I need to get only record from today for specific shop, so my start and end keys are:
startKey = [ product_id, DateTime.Now.Ticks, shop_id]
endKey = [ product_id, DateTime.Today.Ticks, shop_id]
sorting order is descending and row limit is 1.
The problem is that I get a record with completly another shop_id. How can I query such view where 1st and 3rd key is fixed?
You can’t perform that query with that view. The index is sorted by the three array items, with product_id as the primary sort, since as secondary and shop_id as tertiary. Queries return a range of items from the index. If you think about it, by asking for a range that includes multiple
since values, you’ll end up getting records for all the shop_ids that have product_id and since in that range.
You’ll need another view where the key is [product_id, shop_id, since].
I figured out that already.
Apparently I misunderstood docs about querying with multiple keys and applied old sql approach.
Glad you figured it out. In future, it would be good if you can indicate as such or better still, share your solution. That way, the effort is not redundant and it would also help others in the community with similar issue.