Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write server timestamp in milliseconds to Firestore

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.

like image 423
Vincent Avatar asked Dec 31 '17 04:12

Vincent


Video Answer


1 Answers

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.

like image 168
Doug Stevenson Avatar answered Sep 18 '22 14:09

Doug Stevenson