【问题标题】:Why firebase orderByChild doesn't work on timestamp?为什么firebase orderByChild不适用于时间戳?
【发布时间】:2017-11-09 07:35:31
【问题描述】:

我有一个帖子列表。每个帖子在createdAt 中包含firebase servervalue timestamp

假设,我的帖子标题是 1、2、3、4、5。当我做这个查询时。

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

它仍然返回 1,2,3,4,5。我要的是5,4,3,2,1

我尝试导航时间戳并且它有效。为什么?这是否意味着我必须使用另一个带有负时间戳的子 orderTimestamp 进行反向查询?

更新 json

我的意思是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')

orderByChild('o')

【问题讨论】:

  • 请分享您正在查询的 JSON 的 sn-p(作为文本,没有屏幕截图)。您可以通过单击Firebase Database console 中的“导出 JSON”链接来获取此信息。
  • @FrankvanPuffelen 已更新。
  • 我想我明白了。正如@Mathew 的回答,firebase 总是按升序返回,因为 -10 小于 -5,所以它首先返回最新的帖子。

标签: android firebase firebase-realtime-database


【解决方案1】:

它总是以升序返回,在检索后由您在客户端按您想要的方式对其进行排序。如果你有 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 并且你想要 15,14,13,12,11 你会做.limitToLast(5)而是获取 11,12,13,14,15,然后在客户端向后排序。

【讨论】:

  • 好的,我明白了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 2019-06-06
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多