Ad

Is It Ok To Add Timestamp As A Document Field In Firestore Database?

- 1 answer

While working with Cloud Firestore, I read this statement on the Add data to Cloud Firestore Docs:

Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.

So, I designed my database as it contains timestamp field in all the documents.

But after that, I read these statements on the Best Practices Docs:

Be aware that indexing fields with monotonically increasing values, such as timestamps, can lead to hotspots which impact latency for applications with high read and write rates.

Avoid high read or write rates to lexicographically close documents, or your application will experience contention errors. This issue is known as hotspotting, and your application can experience hotspotting if it does any of the following:

  • Creates new documents with a monotonically increasing field, like a timestamp, at a very high rate.

So I am confused now. I am developing a android app. Can i use timestamp as a field? Will it create any problem if there are lots of users for my app? And Yes, I must order the documents by creation date.

Ad

Answer

There's a huge difference between those those 2 pieces of documentation.

The first one is referring to the fact that Cloud Firestore auto-generated IDs do not a have time component as Firebase realtime database push IDs have. That's the reason it cannot provide any automatic ordering. So it has nothing to do with the fact that you have a timestamp as a property of an object.

The second one is referring to the fact that is not recommended to use monotonically increasing values, such as timestamps as IDs of the document. So remember, that's the case only when using such timestamps as keys in the database, only in this case it can lead to hotspots which impact latency.

Since you are using a timestamp as field within each document, there is nothing to worry about. If you have used that timestamp as a document id in Coud Firestore or as a node key in Firebase realtime database, the above rules apply.

Ad
source: stackoverflow.com
Ad