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)});
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(/* ... */)});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With