Tholle
Tholle

Reputation: 112787

RethinkDb: Get X entries before given timestamp

I would like to query my RethinkDB with a timestamp, and in return get the first X entries that came before this timestamp.

What I've tried so far is the following:

r.db('deepstream').table('chat').filter(function (message) {
    return message("_d")("timestamp").lt(<timestamp>);
}).limit(X);

This gives me X entries before the given timestamp, but not in the order I desire.

My entries looks as follows:

{
  "_d": {
    "id":  "chat/ibcta7zt-5k6ldeay2xr" ,
    "message":  "sadas" ,
    "timestamp": 1435274339341 ,
    "user":  "Emil"
  } ,
  "_v": 1 ,
  "ds_id":  "ibcta7zt-5k6ldeay2xr"
}

The first half of the "ds_id" is a base64-encoded timestamp, if that may be to any help.

How can I query the DB to ensure that the result I get are the X previous messages, and not just X random messages that arrived earlier?

Upvotes: 0

Views: 171

Answers (1)

Tholle
Tholle

Reputation: 112787

Ordering the DB in descending order according to the timestamp solved my problem.

r.db('deepstream').table('chat').orderBy({index: r.desc('ds_id')})
  .filter(function (message) {
     return message("_d")("timestamp").lt(<timestamp>);
  }).limit(X);

Upvotes: 1

Related Questions