Access last N documents of Google Firestore collection

Viewed 3099

I want to access last N documents from google firestore collection. The key for documents are timestamp in ISO format. The collection first needs to be sorted on timestamp keys and the last N documents updated are to be retrieved. This operation has to be realtime and fast.

events(collection) =>
documents as below:
2019-01-07T08:21:18.600Z => {
  symbolname: '10900CE',
  price: '10825.0',
  timestamp: '2019-01-07T13:51:18Z',
  quantity: '2.0',
  side: 'SELL' }
2019-01-07T08:21:28.614Z => { 
  symbolname: '10800PE',
  price: '10825.0',
  timestamp: '2019-01-07T13:51:28Z',
  quantity: '2.0',
  side: 'BUY' }
2019-01-07T09:45:24.783Z => { side: 'SELL',
  symbolname: '10800PE',
  price: '10805.0',
  timestamp: '2019-01-07T15:15:24Z',
  quantity: '2.0' }

I currently loop through the collection and add the keys to an array and then sort the array and get last N document by those keys as following:

var ids = []
db.collection('events').get()
  .then((snapshot) => {
    snapshot.forEach((doc) => {
      ids.push(doc.id)
    });
  })
  .then(()=>{
    //sortIds(ids);
    console.log(JSON.stringify(ids[ids.length-2])) //second last entry 2019-01-07T08:21:28.614Z
  })
  .catch((err) => {
    console.log('Error getting documents', err);
  });

This operation takes a long time if collection size increases. Is there an optimal way of achieving this (sorting/orderBy-descending collection and fetch last N documents)?

Thanks

1 Answers

Perhaps something like this:

db.collection('events').orderBy("timestamp").limit(n)
Related