Order Firestore data by TimeStamp in Ascending order

后端 未结 3 816
天涯浪人
天涯浪人 2021-01-03 23:51

I am storing Ideas posted by the application in Firestore. The data is stored in Firestore like this Ideas/{documentID}/IdeaObject. The issue is when I retr

相关标签:
3条回答
  • 2021-01-03 23:58

    You cannot use a String (timeStamp) when querying your database instead of a Date (date) and expect to behave as it was a date. So to solve this, please change the following line of code:

    firestoreDb.collection("ideas")
                .orderBy("timeStamp", Query.Direction.ASCENDING)
    

    to

    firestoreDb.collection("ideas")
                .orderBy("date", Query.Direction.ASCENDING)
    
    0 讨论(0)
  • 2021-01-03 23:58

    You have to also add index in your firebase account which is in database -> Indexes -> add index

    0 讨论(0)
  • 2021-01-04 00:02

    In my case, the vanilla version would be,

    firestoreDb.collection("ideas")
         .orderBy("timestamp", "asc")
    
    firestoreDb.collection("ideas")
         .orderBy("timestamp", "desc")
    

    "ideas" is the name of your collection

    "timestamp" is the key or field or column name to be used for sorting.

    "asc" or "desc" is the option to be used for the order

    0 讨论(0)
提交回复
热议问题