Reputation: 595
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.
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!
These return documents as expected:
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
and
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '<', today).get().then(function(querySnapshot) {
These don't return anything:
var today = new Date()-604800000;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
and
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today-604800000).get().then(function(querySnapshot) {
and just for the heck of it
var today = new Date()-1;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
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)});
Upvotes: 16
Views: 20270
Reputation: 1882
In case this helps anyone querying with a unix timestamp (I'm using seconds), here's how this looks:
.where("timestamp", "<", new Date(endTime * 1000))
Upvotes: 0
Reputation: 595
Ok. So I got some assistance from a very helpful Google developer.
This ended up working.
var beginningDate = Date.now() - 604800000;
var beginningDateObject = new Date(beginningDate);
db.collection("****")
.where('display', '==', true)
.where('createdAt', '>', beginningDateObject)
.get()
.then(function(querySnapshot) {console.log(/* ... */)});
Upvotes: 34