Problem with view using date startkey/endkey inclusive_end (3.0.1 server)

I am having a problem where it appears that the multi-key functionality is not working as expected. Here is my view:

function (doc, meta) {
  if(doc.docType && doc.docType == "dialRequest"){
    if(doc.expireTime){
          var dateArray = dateToArray(doc.expireTime);
        emit(dateArray,null);
    }
  }
}

Here is a snippet of the data that is being returned.

[2013,11,5,14,47,1]
201411050947019990000000000001 
null

[2013,11,5,14,47,1]
201411050947019990000000000003 
null

It doesn’t appear that my range key search is working correctly. If I do

startKey = [2013]
endKey = [2013]
inclusive_end=true

I get 0 records returned. If I change it to

startKey=[2013]
endKey=[2014]
inclusive_end=true

I get the correct result, which just includes records with the year 2013. What am I missing here? This is Couchbase 3.0.1 all through the Console. I get the same results using the Java SDK as well.

Thanks in advance!

Hi, you should use startKey = [2013,0,0,0,0,0] and endKey = [2013,12,31,99,99,99] and this should work.
thanks

Thanks. That seems to work on the Console, but when I try through Java, I get inconsistent results:

This URI returns what I would expect:

http://vmdev-memcache1.connectfirst.com:8092/default/_design/dialrequests/_view/expired_by_server?inclusive_end=true&reduce=false&stale=false&connection_timeout=60000&start_key=[1001,0,0,0,0,0,0]&end_key=[1001,9999,99,99,99,99,99]

This snippet of java code does not, even thought it should be identical:

ViewQuery vq = ViewQuery.from("dialrequests","expired_by_server");
        vq.startKey("[1001,0,0,0,0,0,0]");
        vq.endKey("[1001,9999,99,99,99,99,99]");
        vq.inclusiveEnd(true);
        vq.reduce(false);
        vq.stale(Stale.FALSE);
                        
        ViewResult vr = bucket.query(vq);

I am using the 2.0 Java SDK with Coucbase Server 3.0.1

Also posting it here since you asked the same question in 2 places:

I probably know what’s going on. When you construct a query from the SDK, its very important you get the output JSON right. So here you are inserting a string, which will make it quoted automatically. Both start and endkey also take a JsonArray which should make it similar to your query from the UI.

look at this:

    // what you want
    ViewQuery q1 = ViewQuery
        .from("dialrequests", "expired_by_server")
        .startKey(JsonArray.from(1001, 0, 0, 0, 0, 0, 0))
        .endKey(JsonArray.from(1001, 9999, 99, 99, 99, 99, 99))
        .inclusiveEnd()
        .reduce(false)
        .stale(Stale.FALSE);

    // wrongly quoted
    ViewQuery q2 = ViewQuery
        .from("dialrequests", "expired_by_server")
        .startKey("[1001, 0, 0, 0, 0, 0, 0]")
        .endKey("[1001,9999,99,99,99,99,99]")
        .inclusiveEnd()
        .reduce(false)
        .stale(Stale.FALSE);

    System.out.println(q1);
    System.out.println(q2);

Which prints

reduce=false&stale=false&inclusive_end=true&startkey=%5B1001%2C0%2C0%2C0%2C0%2C0%2C0%5D&endkey=%5B1001%2C9999%2C99%2C99%2C99%2C99%2C99%5D
reduce=false&stale=false&inclusive_end=true&startkey=%22%5B1001%2C+0%2C+0%2C+0%2C+0%2C+0%2C+0%5D%22&endkey=%22%5B1001%2C9999%2C99%2C99%2C99%2C99%2C99%5D%22

(note the %22 in the start and endkeys to wrongly quote)

Thanks. I will give that a try this morning.

Sorry about the duplicate post. I realized after I asked in the server forum that there was a JDK specific forum.

Your solution worked great!

Hi Daschl,

I am using couchbase 6x and java sdk 3x.

My question is I am getting below response as params from viewoptions for dates.

StartKey :- [“CLIC”,[2022,10,4]]
EndKey :- [“CLIC”,[2022,11,4,23,59,59]]

ViewQuery{params=“startkey=%5B%22%5B%5C%22CLIC%5C%22%2C%5B2022%2C10%2C4%5D%5D%22%5D&endkey=%5B%22%5B%5C%22CLIC%5C%22%2C%5B2022%2C11%2C4%2C23%2C59%2C59%5D%5D%22%5D&inclusive_end=true&stale=false”}

My code:-
ViewOptions viewOptions = ViewOptions.viewOptions();
viewOptions.startKey(JsonArray.from(startKey)).endKey(JsonArray.from(emdKey)).
inclusiveEnd(true).scanConsistency(ViewScanConsistency.REQUEST_PLUS);

ViewResult viewResults = bucket.viewQuery(viewObj.getDesignDoc(), viewObj.getViewName(),viewOptions);

Output:- {“total_rows”:70,“rows”:[
]
}

What wrong my code, please help me.

Regards,
Potharaju