how to write a query get Firestore current server time using firebase java admin sdk

▼魔方 西西 提交于 2019-12-11 08:48:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!