问题
I want to write a query to get a list of document which was saved in the past 24 hours. When the document was created, I add a "last updated" value using FieldValue.serverTimestamp(). Now my query would be like:
Date pastDay = (getFireStoreServerTime) - (24hours)
Query query = collectionRef.orderBy("lastUpdated", Query.Direction.DESCENDING).where("last updated" > pastDay);
However, I wasn't able to find the API in java admin SDK to get the server time. Is this supported in Firebase Admin SDK for Cloud FireStore?
回答1:
Firestore doesn't have an API that just exposes the server time. The Server SDKs do expose a "read time" however, which you could (ab)use to fetch the current time of the backend.
DocumentSnapshot doc = firestore.doc("coll/nonexistingdocument").get();
Instant instant = doc.getReadTime();
Date pastDay = (instant - 24hours);
You can do the same thing with a QuerySnapshot, but that is a more expensive operation.
来源:https://stackoverflow.com/questions/50343038/how-to-write-a-query-get-firestore-current-server-time-using-firebase-java-admin