12
votes

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)});
1
Are you sure the date object represents "7 days ago"? - Alex Mamo
At this point, I'm just trying to get it to filter, so I'm not sure if the actual number matters. Definitely tried with other numbers. When I show the today var after modification, it shows correctly. - Adam
You cannot filter using numbers, you should filter using Date objects. And you must be sure that you have records in your database that are older that "7 days ago" and the date object that is passed represents "7 days ago", right? - Alex Mamo
Ok, so no filtering using numbers. I will remember that, thanks. Definitely checked the database and there are records going back awhile. Also added some for the future to check that things were pulling in that direction. Am I creating the date object incorrectly here var today = new Date()-604800000? - Adam

1 Answers

25
votes

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(/* ... */)});