1
votes

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
Have you read the documentation about this? There's a whole page that describes sorting and limiting. firebase.google.com/docs/firestore/query-data/order-limit-data - Doug Stevenson
I went through this but couldn't find a way to orderByKey. I could order by key in firebase realtime DB though using something like dbRef.orderByKey().limitToLast(n); Is there a way to order documents by key without adding the key into object? - Aditya Mertia
It looks like you already have a timestamp field in the document. Are you saying you can't use that? - Doug Stevenson
ya that timestamp is thirdparty timestamp for some other purpose. The timestamp in the key is to make it unique entry based on timestamp when object was added to db. So are you suggesting that i need to keep this timestamp as well as part of the object? - Aditya Mertia
Use FieldPath.documentId as the sort key. firebase.google.com/docs/reference/js/… - Doug Stevenson

1 Answers

1
votes

Perhaps something like this:

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