如何编写查询使用Firebase Java Admin SDK获取Firestore当前服务器时间 [英] how to write a query get Firestore current server time using firebase java admin sdk

查看:111
本文介绍了如何编写查询使用Firebase Java Admin SDK获取Firestore当前服务器时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个查询来获取过去24小时内保存的文档列表.创建文档后,我使用FieldValue.serverTimestamp()添加了最后更新"值.现在我的查询将是:

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);

但是,我无法在java admin SDK中找到API来获取服务器时间. Cloud FireStore的Firebase Admin SDK支持此功能吗?

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?

推荐答案

Firestore没有仅公开服务器时间的API.但是,Server SDK确实会公开一个读取时间",您可以(滥用)该时间来获取后端的当前时间.

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);

您可以使用QuerySnapshot做同样的事情,但这是更昂贵的操作.

You can do the same thing with a QuerySnapshot, but that is a more expensive operation.

这篇关于如何编写查询使用Firebase Java Admin SDK获取Firestore当前服务器时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆