[英]Firestore get last 24 hours documents from collection - Node
I want to get the documents which comes under last 24 hours, for each document I have set a field timestamp
as:我想获取过去 24 小时内的文档,对于每个文档,我将字段timestamp
设置为:
admin.firestore().collection('users').set({..., timestamp: new Date().toJSON() })
Now, in my cloud function, I want to get the docs which comes under last 24 hours only, so for that I came up with the logic:现在,在我的云功能中,我只想获取最近 24 小时内的文档,因此我想出了以下逻辑:
const end = new Date();
const start = new Date(end.getTime() - (24 * 60 * 60 * 1000)); // 24 hours ago
const users = await admin
.firestore()
.collection('users')
.where('timestamp', '>', start)
.where('timestamp', '<', end)
.get();
But this returns nothing.但这没有任何回报。
I also tried this question which was asked earlier: Firebase query data of last 24 hour return same result我也尝试过之前提出的这个问题:过去 24 小时的 Firebase 查询数据返回相同的结果
const users = await admin
.firestore()
.collection('users')
.startAt(start)
.get();
But it resulted in the following error:但它导致了以下错误:
ERROR Error: Too many cursor values specified. The specified values must match the orderBy() constraints of the query.
So, can someone help me out here?那么,有人可以在这里帮助我吗?
Thanks in advance!提前致谢!
(Do I have to change how I save timestamp
in firestore? like instead of timestamp: new Date().toJSON()
something else...?) (我是否必须更改在 Firestore 中保存timestamp
的方式?比如代替timestamp: new Date().toJSON()
其他...?)
EDIT-1 :编辑-1 :
Since the timestamp
field in your database is stored as a string value, you also have to pass string values for conditions on that field:由于数据库中的timestamp
字段存储为字符串值,因此您还必须为该字段的条件传递字符串值:
admin
.firestore()
.collection('users')
.where('timestamp', '>', start.toJSON())
.where('timestamp', '<', end.toJSON())
Without that you're comparing strings and dates, and no value will every meet the condition in that case.否则,您将比较字符串和日期,并且在这种情况下,没有任何值都符合条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.