Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to order by timestamp using Firebase?

I'm trying to order by timestamp in Firebase using orderByChild() but it's not working. The results seem to be ordered by the key and Firebase seems to be ignoring orderByChild().

As you can see from the time, these results are not ordered by timestamp.

lorem-ipsum-dolor-si - 17/6/2016 a las 8:51:33

lorem-ipsum-dolor-si193 - 17/6/2016 a las 8:51:37

lorem-ipsum-dolor-si297 - 17/6/2016 a las 8:51:43

lorem-ipsum-dolor-si402 - 17/6/2016 a las 8:51:38

This is how I get the data:

let endpoint = 'texts/-KKPSjKwelbXG6Rq8AEh/-KKQqtg_a95p4Gm13XgZ'
firebase.database().ref(endpoint).orderByChild('timestamp').on('value',...)

And since snapshot.val() will be ordered by the keys, and not by the orderByChild() order, I have to store the snapshot ordered in an array and send it back to the view.

this.data = []

snapshot.forEach(function(child) {
    this.data.push(child.val())
}.bind(this))

return this.data

The timestamp comes from Date.now(). I've tried storing it as Number or String with the same result.

It's storing fine in Firebase as you can see:

{
  "-KKPSjKwelbXG6Rq8AEh" : {
    "-KKQqtg_a95p4Gm13XgZ" : {
      "lorem-ipsum-dolor-si" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171493149
      },
      "lorem-ipsum-dolor-si193" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171497193
      },
      "lorem-ipsum-dolor-si297" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171503297
      },
      "lorem-ipsum-dolor-si402" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171498402
      }
    }
  }
}

What am I missing?

Edit:

The problem was that I was creating the reference and later adding orderByChild().

let ref = firebase.database().ref(endpoint)
...
ref.orderByChild('timestamp')
...
ref.on('value',onChange)

I was doing that because I have a class that creates these calls to Firebase dynamically.

like image 801
Pier Avatar asked Jun 17 '16 14:06

Pier


People also ask

How do I reorder documents in Firebase?

You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() . Note: An orderBy() clause also filters for existence of the given field.

What is timestamp in Firebase?

What is timestamp in firebase? A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.

Does Firebase preserve order?

So: yes, the items in an array field in a DocumentSnapshot that you read from Firestore are in the same order that you added them to the array.

What is the format of firestore timestamp?

Date and time in the Firestore are saved in their proprietary date format — called Timestamp. Firestore timestamp is just a simple object with two properties — seconds and nanoseconds, which apparently has some advantages over “classic” date format.


1 Answers

Works for me:

firebase.database().ref(endpoint)
  .orderByChild('timestamp')
  .on('value', function(snapshot) {
  this.data = [];

  snapshot.forEach(function(child) {
    this.data.push(child.val());
  }.bind(this));

  console.log("all", data.map(function(val) { return new Date(val.timestamp).toString(); }));
});

"all"

["Fri Jun 17 2016 06:51:33 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:37 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:38 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:43 GMT-0700 (PDT)"] Blockquote

firebase.database().ref(endpoint)
  .orderByChild('timestamp')
  .startAt(1466171497190)
  .endAt(1466171498405)
  .on('value', function(snapshot) {
  this.data = [];

  snapshot.forEach(function(child) {
    this.data.push(child.val());
  }.bind(this));

  console.log("filtered", data.map(function(val) { return new Date(val.timestamp).toString(); }));

});

"filtered"

["Fri Jun 17 2016 06:51:37 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:38 GMT-0700 (PDT)"]

http://jsbin.com/humatuquju/edit?js,console

like image 170
Frank van Puffelen Avatar answered Sep 24 '22 07:09

Frank van Puffelen