java - 从 Android 中的 Firestore 获取过去 24 小时的文档
问题描述
我正在尝试在我的 Android 应用程序中从 Firestore 获取过去 24 小时的文档。就我而言,文件是邮政持有人。每个帖子文档都有一个包含 Firestore 时间戳的字段。有没有办法在查询中比较时间戳和日期?我将使用设备日期与时间戳进行比较。如果当时的设备日期不正确怎么办?
我的 Firestore 看起来像:
--- posts (collection)
| |
| --- postid (documents)
| |
| --- country_code: "TR"
| |
| --- timestamp: Firestore.Timestamp
| |
| --- post_text: "Some Text"
| |
| --- username: "username"
解决方案
有没有办法在查询中比较时间戳和日期?我将使用设备日期与时间戳进行比较。
那当然是。解决此问题的最简单方法是将用户的日期作为Date类型的对象并执行如下所示的查询:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference postsRef = rootRef.collection("posts");
Query queryByTimestamp = postsRef.whereGreaterThan("timestamp", yourDateObject - 24h);
queryByTimestamp.get().addOnCompleteListener(/* ... /*);
我在您的屏幕截图中看到的内容:
timestamp: "Server.Timestamp"
它肯定行不通。timestamp 属性应包含 Date 或 Firestore 时间戳,如我在以下帖子中的回答中所述:
所以永远不要将其添加Server.Timestamp
为字符串。
推荐阅读
- laravel - Laravel:不允许多个用户更新同一数据库记录
- java - IntelliJ IDEA 中公共 setter/mutator 方法的“访问可以是私有的”警告消息
- angular - 角度材料:如何在两个输入垫选择之间调整参考
- java - 从第二类访问后,两个类中的自动装配 Bean 未更新
- javascript - ReactJS 中的 MP3 编码
- mongodb - Mongo $addField 用于嵌套结构化数据
- terminal - 如何通过网页实时共享终端命令输出?
- python - python中句子的正则表达式
- sql - 更新块的顶部
- elasticsearch - ElasticSearch:搜索结果中的嵌套项计数