Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couchbase query based on time

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.

Here is what I have so far:

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.

 function (doc, meta) 
{
  if(meta.type == "json" && doc.type == "POST" && doc.created != null) 
  {
    emit([dateToArray(doc.created), doc.visibility], null);
  }
}

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.

 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);

EDIT:

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.

** Update **

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.

like image 958
Michael Avatar asked Nov 11 '22 18:11

Michael


1 Answers

You need to reorder and emit your compound key like so:

function (doc, meta) {
  if(meta.type == "json" {
    if(doc.type == "POST") {
      emit([doc.visibility,dateToArray(doc.created)], null);    
    }
  }
}

Which will produce keys like: [true,[2013,11,8,20,0,0]]

Pass in a startKey and endKey in the format like above and that will filter all documents since the startKey date that are either doc.visibility False or True (depending on your start key), I assume you only want doc.visibility == true.

Your return keys will be ordered by the first part of the compound key, so the first result in the ViewResult will actually be the oldest document returned. So you will have to reverse the array in your application logic if you want the most recent document first.

Remember to take into consideration the stale parameter if you are building a 'live' feed.

like image 170
scalabilitysolved Avatar answered Nov 15 '22 06:11

scalabilitysolved