Firebase如何通过createdAt字段对集合进行排序?

时间:2016-10-25 05:10:28

标签: javascript firebase firebase-realtime-database

我有一个看起来像这样的集合模型:

enter image description here

如何通过createdAt字段将查询从最新到最旧排序。

到目前为止,我试过

db.ref('accounts')
    .startAt()
    .orderByChild('createdAt')
    .once('value')
    .then(function(snap){
        // do shits here...
    })

但我得到的数据是最老的。

  1. 一月
  2. 二月
  3. 三月
  4. 但我想要

    1. 三月
    2. 二月
    3. 一月

1 个答案:

答案 0 :(得分:1)

  1. 没有方法可以在firebase数据库中按降序对项目进行排序。因此,您需要通过在编程语言中升序和反转数组来对它们进行排序。

  2. 如果要按时间对它们进行排序,则应使用timestamp而不是datetime作为字符串。在这种情况下,您将得到错误的排序,因为它们实际上被比较为字符串。例如:想象一下这两个日期UPDATE FundDetails SET Column1 = CASE WHEN (funddetails.Industry * t1.price- t2.MyAvg) <= -5 THEN '50' ELSE '100' END FROM (SELECT fd.*, Avg(fd.Industry) OVER (partition BY fd.state ) MyAvg FROM FundDetails fd) t2 INNER JOIN IndustryDetails t1 on t1.State = t2.State --WHERE FundDetails.Id = t2.Id 2016-10-14T02:30:59.653Z。作为日期,第一个更大,但作为字符串(按字典顺序),第二个更大。