firebase.firestore.FieldValue.serverTimestamp() as int

If I use the following command in my firestore:


Im saving the serverTimestamp but in a timestamp format. How can I make it so it resembles the same format as a number created by ?

What I want to accomplish is to save the serverTimestamp() but in a plain integer format so basicly save the serverTimestamp of the firestore in miliseconds – very important.


Im creating a chat app using firestore and my query looks like this:

var loadnewmessages = messagesRef.orderBy("timestamp").where("timestamp", ">", parseInt(lasttimekey)).limit(50).onSnapshot((snapshot)  => {//listener for new messages
            var newmessages = lastchatbackup;
            //console.log("Loaded from backup: ", newmessages)
            var deletedmesssages = [];
            snapshot.docChanges().forEach((change) => {
                if(change.type  === "added"){...

lastchatbackup is in this case my backup which Im always saving after a new message arrived (entire chat), also Im storing the lasttimekey which is the timestamp of the most recent message arrived (this is only a integer, nothing special).

The problem is, if I have 2 devices which do not have the exact same time, Im loading the chat backup and loading messages which should not be loaded anymore (because the other phone had another then the phone Im using now. Result is, Im having dublicates displayed.

This is the reason I can not use anymore in my case and why I want to use the firestore.FieldValue.serverTimestamp(). However now I have the problem that I am filtering in my query all messages out which are older then the most recent message im my backup, so I only receive the messages I do not have on my phone in the backup yet.

I can only use strings in the .where() statement in the query, so how I am supposed to filter by the timestamp when I can not properly inserty anymore a usual ( anymore? Is there a way to implement filtering a query based on the timestamp so basicly filter out older messages? If there is one, in which format should I parse them into the .where() statement in the query call?


You can’t change how a FieldValue.serverTimestamp() saves its data, which is as a Timestamp object. The big difference between the two is that Firestore’s Timestamp objects have nano-second precision, while JavaScript’s only has millisecond precision.

What you can do however it get only the milliseconds from the Firestore Timestamp object by calling toMillis() on it.