How to apply filter in Firebase database query for timestamp

1,887 views
Skip to first unread message

Faisal Shaikh

unread,
Jan 4, 2018, 11:01:47 AM1/4/18
to Firebase Google Group
Is there any way that I can fetch all the items from firebase database if the timestamp is lower than database timestamp. Basically, I am storing a timestamp in the database and want to fetch data by comparing timestamp.

Brian Woodward

unread,
Jan 4, 2018, 11:37:08 AM1/4/18
to fireba...@googlegroups.com

This may have changed since I wrote this code, but the following is a simple utility function for getting the "remoteDate" by using the server's TimeOffset:

var remoteDate = null;
utils.remoteDate = function(ref) {
  if (remoteDate) return remoteDate;

  var offset = 0;
  ref.child('/.info/serverTimeOffset').on('value', function(snapshot) {
    offset = snapshot.val() || 0;
  });

  remoteDate = function() {
    return Date.now() + offset;
  };

  return remoteDate;
};

This function returns a function that uses the offset to get the remote date:

this.remoteDate = utils.remoteDate(this.root);

Then I use `this.remoteDate` when querying the database:

  var now = this.remoteDate();
  this.ref.orderByChild('nextRun')
    .endAt(now)
    .once('value', function(snapshot) {
      cb(null, snapshot.val());
    });

`nextRun` in the `orderByChild` above is also a timestamp, so I'm looking for all the records from before the current time.

Hope this helps. The code can be found in firebase-cron if you need a closer look.

Thanks,
Brian

On Thu, Jan 4, 2018 at 9:24 AM, Faisal Shaikh <f.shai...@gmail.com> wrote:
Is there any way that I can fetch all the items from firebase database if the timestamp is lower than database timestamp. Basically, I am storing a timestamp in the database and want to fetch data by comparing timestamp.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/c2ea9d6c-426f-4bf8-af9c-9466de3b14d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages