在时间戳字段上查询Firestore数据库 [英] Query firestore database on timestamp field
问题描述
我对Firestore(和编程)还比较陌生,无法在线找到解决我问题的方法.
I'm relatively new with firestore (and programming) and wasn't able to find a solution to my problem online.
试图查询现有集合中的文档.所有文档都有一个时间戳字段.
Looking to query documents in an existing collection. The documents all have a timestamp field.
当我尝试查询所有文档>"或<"时现在,查询工作正常.当我尝试查询>"或<"时7天前,查询未返回任何内容.我确定我可能只是想念一些小东西.感谢您的帮助!
When I attempt to query for all documents ">" or "<" right now, the query works fine. When I attempt to query for ">" or "<" 7 days ago, the query returns nothing. I'm sure that I'm probably just missing something small. Thanks for any help!
这些预期的归还文件:
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
和
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '<', today).get().then(function(querySnapshot) {
这些都不返回任何内容:
var today = new Date()-604800000;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
和
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today-604800000).get().then(function(querySnapshot) {
只是为了实现它
var today = new Date()-1;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
我已经看到其他人要求查看Firestore中的字段,所以下面是一张图片: 很抱歉,这是一个链接
I've seen others request a look at what the field looks like in Firestore so here is a pic: sorry it's a link
请告诉我是否还有其他可能有用的信息.谢谢!
Please let me know if there is anything else that might be helpful. Thanks!
编辑以显示下一个尝试:
EDIT TO SHOW NEXT ATTEMPT:
var config = {****};
firebase.initializeApp(config);
const db = firebase.firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};
db.settings(settings);
var today = new Date();
var yesterday = date.setDate(today.getDate() - 1);
db.collection("****")
.where('display', '==', true)
.where('createdAt', '>', yesterday)
.get()
.then(function(querySnapshot) {console.log(createdAt)});
推荐答案
好.因此,我从非常有帮助的Google开发人员那里获得了一些帮助.
Ok. So I got some assistance from a very helpful Google developer.
这最终奏效了.
var beginningDate = Date.now() - 604800000;
var beginningDateObject = new Date(beginningDate);
db.collection("****")
.where('display', '==', true)
.where('createdAt', '>', beginningDateObject)
.get()
.then(function(querySnapshot) {console.log(/* ... */)});
这篇关于在时间戳字段上查询Firestore数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!