How can we sort Firebase Firestore data by TimeStamp dataType using Angular?

Viewed 6755

I am having Ticket Posting Web App, and i would show only those tickets which are active and with-in 24 hours records(from now on).

I am using Firebase Cloud Firestore with Angular 7.

 getTicketList(){
 this.ticketData = 
 this.fireStore.collection('ticket').doc("paid").collection("ticket-list", 
 ref =>
 ref.where('status', '==', 
  "active").where("paid","==","false").orderBy("myTimeStamp");
 return this.ticketData.snapshotChanges() ;
 }

We are having a a key "myTimestamp" which is Firestore Timestamp Type.

want to show only those records which are active and currently having a time remaining to book.

Beow i mentioned the codes which i am using to get from Firebase service.

 this.firestore.getTicketList().subscribe(list => {
  this.ticketList = list.map(item => {
    return { id:item.payload.doc.id,
      ...item.payload.doc.data()}
  })
  console.log(this.ticketList);
});

Thanks

Edited

enter image description here enter image description here

1 Answers

You can query on a data/timestamp field by passing a Date object. So for example to get all items before right now:

ref
  .where('status', '==', "active")
  .where("paid","==","false")
  .where("myTimeStamp","<", new Date())
  .orderBy("myTimeStamp")
Related