My CouchDB document format as below and based on the price changes there can be multiple documents with same product_id & store_id
{
"_id": "6b645d3b173b4776db38eb9fe6014a4c",
"_rev": "1-86a1d9f0af09beaa38b6fbc3095f06a8",
"product_id": "6b645d3b173b4776db38eb9fe60148ab",
"store_id": "0364e82c13b66325ee86f99f53049d39",
"price": "12000",
"currency": "AUD_$",
"time": 1579000390326
}
and I need to get the latest document (by time - the timestamp) for given product_id & store_id
For this, with my current solution I have to do two queries as below;
To get the latest timestamp. This returns the latest timestamp for given
product_id&store_id"max_time_by_product_store_id": { "reduce": "function(keys, values) {var ids = [] values.forEach(function(time) { if (!isNaN(time)){ ids.push(time); } }); return Math.max.apply(Math, ids) }", "map": "function (doc) {emit([doc.store_id, doc.product_id], doc.time);}" }Based on the latest
timestamp, again I query to get the document with three parameters that arestore_id,product_id&timeas below,"store_product_time": { "map": "function (doc) { emit([doc.store_id, doc.product_id, doc.time]); }" }
This works perfectly for me but my problem is I need to do two DB queries to get the document and looking for a solution to fetch the document within one DB query.
In CouchDB selector also has no way to get the document by MAX value.
With CouchDB's /db/_find, you can descending
sortthe result andlimitthe result to one document as follows:CURL