I know how to write the server timestamp to Firestore as a date string:
doc_ref.set({ts: firebase.firestore.FieldValue.serverTimestamp()});
but how can I store its unix timestamp as a number instead? Is this even possible? I need to be able to mirror a subset of my Firestore documents in Algolia and I don't believe I can sort an Algolia index by timestamp strings.
It's not being stored as a string in the document - that's just how it serverTimestamp renders in the console. If you examine the ts field more closely, you'll see that it has a type of "timestamp". When you go to edit the value in the console by clicking it, you'll see the type a value clearly shown as a date type. When you read it back out (in JavaScript), you'll get a Date object back, and you can get the unix epoch-based time from that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With