vzhen
vzhen

Reputation: 11157

Why firebase orderByChild doesn't work on timestamp?

I have a list of post. Each post contain firebase servervalue timestamp in createdAt.

let say, my post titles are 1,2,3,4,5. When i do this query.

const userPostsRef = database.ref(`userPosts/${uid}`)
                    .orderByChild('createdAt').limitToLast(10);`

And it still return 1,2,3,4,5. What I want is 5,4,3,2,1

I tried to navigative timestamp and it works. Why? and is that mean i have to use another child orderTimestamp with negative timestamp for reverse query?

Update json

I mean limitToLast();

userPosts
 sms|5797235fe618d33da2eb0ad3

  -Km2l4HHE3pONRnA2Akp
   createdAt: 1496859301233
   o: -1496859301233
   title: "2"

 -Km2l5QQRi1F66LJkM45
  createdAt: 1496859305916
  o: -1496859305916
  title: "3"

 -Km2uTa8tAe0G5cJ2U-B
  createdAt: 1496861764218
  o: -1496861764218
  title: "4"

 -Km2uUrfwvY90Im06kg4
  createdAt: 1496861769435
  o: -1496861769435
  title: "5"

orderByChild('createdAt')

enter image description here

orderByChild('o')

enter image description here

Upvotes: 0

Views: 373

Answers (1)

Mathew Berg
Mathew Berg

Reputation: 28750

It always returns in ascending order, it is up to you on the client to order it the way you want after retrieval. If you had 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 and you wanted 15,14,13,12,11 you would do .limitToLast(5) instead getting 11,12,13,14,15 and then ordering backwards on the client.

Upvotes: 2

Related Questions