Question

Having this document structure:

"document":{
    "title":"Cheese ",
     "start_date": "2010-02-17T15:35:00",
     "source": "s_source1"
}

I would like to create a view that return all documents ids between 2 dates and for a certain source:

function (doc, meta) {
      emit([doc.start_date,doc.source], null);
}

I tried to use this range key to get all document of source 1 between "2014-04-04" and "2014-04-05":

startkey=["2014-04-04","s_source1"]&endkey=["2014-04-05","s_source1"]

But this don't work for the sources. It retrieves me all document for the date range but for all sources ( s_source1,s_source2,...) .

I guess that the underscore is the source of problem( some encoding issue)?

How should I set my key range to get only documents of a unique source for a certain date range?

Was it helpful?

Solution

If you reverse your compound key then you'll be able to do the select, keys sort from left to right in Couchbase.

function (doc, meta) {
  if(meta.type == "json") {
    if(doc.start_date && doc.source) {
      emit([doc.source,dateToArray(doc.start_date)],null);    
    }
  } 
}

To select all documents with a source value of: "s_source1" since a date in 2010 until the present day you'd have your keys like so:

Start_Key: ["s_source1",[2010,2,18,15,35,0]]
End_key:   ["s_source1",[2014,2,18,15,35,0]]

This question on the Couchbase website has some fantastic explanations of compound key sorting, I'd thoroughly recommend reading it: http://www.couchbase.com/communities/q-and-a/couchbase-view-composite-keys

Plus here is an informative section from the official documentation: http://docs.couchbase.com/couchbase-manual-2.0/#selecting-information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top