How does insertion/retrieval ordering work in Firestore?

529 views
Skip to first unread message

colto...@protonmail.com

unread,
Apr 16, 2019, 12:55:41 PM4/16/19
to Firebase Google Group
I have a fairly simple example project I'm working on. I have a root collection that holds "tweet" documents. I want to have an api using cloud functions that allows someone to get the last 10 tweets. 


I have seen that I can limit my results fairly easily. What I didn't see is any guarantee in terms of ordering in terms of insertion into Firestore. For my example, in my project I don't actually save the timestamp of a tweet, and so this may be problematic? In a relational database, I could rely on the insertion order, and just say "Give me the last 10 tweets from my tweets table"

In Firestore there doesn't seem to be any guarantee what order items are inserted, therefore, what way they come back. Can anyone shed some light on this?




Sam Stern

unread,
Apr 16, 2019, 1:03:20 PM4/16/19
to Firebase Google Group
Hi there,

You are correct.  If you want to order data by timestamps then you will need to add an explicit timestamp field to your documents before adding them to Cloud Firestore.  You can either set this value to the time of the tweet, or use Firestore's "server timestamp" to record the time of insertion:


- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/37f6086f-49c5-4b2e-b7dd-5eb29a7aea92%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

colto...@protonmail.com

unread,
Apr 16, 2019, 9:03:15 PM4/16/19
to Firebase Google Group
Thank you for confirming.


On Tuesday, April 16, 2019 at 1:03:20 PM UTC-4, Sam Stern wrote:
Hi there,

You are correct.  If you want to order data by timestamps then you will need to add an explicit timestamp field to your documents before adding them to Cloud Firestore.  You can either set this value to the time of the tweet, or use Firestore's "server timestamp" to record the time of insertion:


- Sam

On Tue, Apr 16, 2019 at 9:55 AM coltonidle via Firebase Google Group <fireba...@googlegroups.com> wrote:
I have a fairly simple example project I'm working on. I have a root collection that holds "tweet" documents. I want to have an api using cloud functions that allows someone to get the last 10 tweets. 


I have seen that I can limit my results fairly easily. What I didn't see is any guarantee in terms of ordering in terms of insertion into Firestore. For my example, in my project I don't actually save the timestamp of a tweet, and so this may be problematic? In a relational database, I could rely on the insertion order, and just say "Give me the last 10 tweets from my tweets table"

In Firestore there doesn't seem to be any guarantee what order items are inserted, therefore, what way they come back. Can anyone shed some light on this?




--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages