Limit and sort by various properties on the couch

Given a couponbase JSON document, for example, collections of milestones similar to this:

{ "milestoneDate" : /Date(1335191824495+0100)/, "companyId" : 43, "ownerUserId": 475, "participants" : [ { "userId": 2, "docId" : "132546" }, { "userId": 67, "docId" : "153" } ] } 

If I chose all the stages of company 43 and would like to order them according to the latest data ... my view on couchbase would look like this:

 function (doc, meta) { if(doc.companyId && doc.milestoneDate) { //key made up of date particles + company id var eventKey = dateToArray(new Date(parseInt(doc.milestoneDate.substr(6)))); eventKey.push(doc.companyId); emit(eventKey, null); } } 

I get both dates and company id on the remaining URLs. However, being completely new to couchbase, I cannot decide how to limit the presentation to returning only company stages 43

The return key is similar to this:

 "key":[2013,6,19,16,11,25,14] 

where the last element ( 14 ) is the company identifier. This is clearly wrong.

The request parameters I tried:

  • &descending=true&startkey=[{},43]
  • &descending=true&startkey=[{},43]&endKey=[{},43]
  • tried to add companyId to value , but could not limit the return results by value.

And according to couchbase documentation, I need the date details at the beginning to sort them. How can I limit them to company ID, please?

thanks.

+4
source share
1 answer

Put the company id at the beginning of the array, and because you will restrict the company id, couchbase sorts by company id and then by date of the array so that you only ever receive documents from one company milestone.

I would change my mind to emit

 emit([doc.copmanyId, eventKey], null); 

and then you can request a view using

 &descending=true&startkey=[43,{}] 

This is what used to work for me.


I came back and tried it with the end key , and this seems to work - restricts and orders as needed:

 &descending=true&startkey=[43,{}]&endkey=[42,{}] 

or

 &descending=true&startkey=[43,{}]&endkey=[43,{}]&inclusive_end=true 

either specify the following incremental / reduced value (based on the descending flag) with the end key or use the same endkey as startkey and set inclusiveEnd to true

Both of these options should work fine. (I tested only those with endkey=42 , but both of them should work)

+5
source

Source: https://habr.com/ru/post/1487861/


All Articles