title: Couchbase query based on time
body: <p>I've been trying to figure out how to get data back based on a timestamp. Basically I want to query 100 docs based on a timestamp field. the 100 docs should be older than the timestamp I passed it.  Also I want to create a refresh where I can pass a timestamp and I get 100 newer docs. Obviously logic would be different here, but having difficulty figuring out how Couchbase can accomplish this. </p>
  
  <p>Here is what I have so far:</p>
  
  <p>My view, as you can see I also need to use a complex key because I'm not only checking date but my visibility field. I do not having anything in the reduce part of the view.</p>
  
  <pre><code> function (doc, meta) 
  {
    if(meta.type == "json" &amp;&amp; doc.type == "POST" &amp;&amp; doc.created != null) 
    {
      emit([dateToArray(doc.created), doc.visibility], null);
    }
  }
  </code></pre>
  
  <p>I am querying using the java client, limit is set to 2 for testing. What is strange here is that setDescending has to be false to get anything back. Also it doesn't matter which date I pass in I always get a result. What I want as expected behaviour is to pass a date and only get results equal or older than the date. </p>
  
  <pre><code> View view = client.getView("dev_posts", "post_list");
    ComplexKey keys = ComplexKey.of(DataConstants.getDateAsArray(startDate), postType);
  
    Query query = new Query();
    query.setRangeStart(keys);
    query.setIncludeDocs(true);
    query.setLimit(2);
    query.setDescending(false);
  
    ViewResponse response = client.query(view, query);
  </code></pre>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>Essentially what I'm looking for from Couchbase is similar to Facebook, Pintrest etc. mobile apps. On a given timestamp when a user refreshes I want to get anything newer. When a user is scroll I want to get the next group past a particular date. </p>
  
  <p>** Update ** </p>
  
  <p>So this has been solved, but going to investigate it further. The ComplexKey is turning our date array into "[2013,11,8,20,0,0]" instead of leaving it [2013,11,8,20,0,0]. Going to investigate it further. Work around for now was not to use to ComplexKey but create our own complex key and pass it to startKey as just a Key. </p>