-1
query(meetingsCollection, where("start", "in", today), orderBy("start", "asc")

I'd like a Firestore query, so that I can fetch all documents between 02/12/2022 - 00:00:00.000 && 03/12/2022 00:00:00.000, ordered by the Date property start, so that I can bucketsize my firestore data request and limit overreads, then use said documents to visually filter accordingly via the front-end.

Can you assist me with this?

  • Not sure if you were aware of it but the Firebase Getting Started Guide [Queries](https://firebase.google.com/docs/firestore/query-data/queries#query_operators) covers how to create a query very similar to the code you're asking us to write for you. See the docs Also be sure questions are clear; 'between' would not include the date, so it would be greater than (>) one date and less than (<) the other. If you want to include that date (inclusive) it would be greater than or equal to (>=) and less than or equal to (<=) – Jay Dec 03 '22 at 14:43
  • You can't filter > on one field and < on another. Comparisons can only run on one field. Quote: "In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field." - But thanks for your comment... – David Griffin Dec 03 '22 at 23:06
  • Thanks and I am aware of that, lol (7 years working with Firebase). I was referring to the two *dates* not two *fields* – Jay Dec 03 '22 at 23:28
  • I am asking for clarity if you wanted to query between two dates inclusive or exclusive to further refine an answer to match the question if needed. – Jay Dec 05 '22 at 18:18

3 Answers3

1

How to get start and end of day in Javascript?

const start = new Date();
start.setUTCHours(0,0,0,0);

const end = new Date();
end.setUTCHours(23,59,59,999);

Firestore query by date range

query(
  meetingsCollection,
  where("start", ">=", start),
  where("start", "<=", end)
  orderBy("start", "asc")
)
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • 1
    Thanks for the links. using those, I was able to deduct a solution. Could probably be a little neater, but it does the trick, and allows me to do what I need to do. – David Griffin Dec 03 '22 at 23:13
1
  db.collection("collection_name")
  .where("start", ">=", "02/12/2022 00:00:00.000")
  .where("start", "<=", "03/12/2022 00:00:00.000")
  .orderBy("start")
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // doc.data() contains the data for the document
    });
  });
  • Unfortunately that solution uses an old version of Firestore, I was looking to build using v9. However, thanks for the lead, it helped to deduct the solution for v9. – David Griffin Dec 05 '22 at 01:14
0

For anyone looking for the actual solution =>>

const now = new Date();

const getDateFormatted = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate().toString().padStart(2, "0");
  return `${year}-${month}-${day}`;
} 

const startNow = new Date(`${getDateFormatted(now)}T00:00:00.000z`); //2021-09-01T00:00:00.000Z
const endNow = new Date(`${getDateFormatted(now)}T23:59:59.000z`);

const nowEvents = ref([]);

const getNowEvents = async () => {
  onSnapshot(myEvents(startNow, endNow), (querySnapshot) => {
    let tmpEvents = [];
    querySnapshot.forEach((doc) => {
        let event = {
          id: doc.id,
          ...doc.data(),
        };
        tmpEvents.push(event);
    });
    nowEvents.value = tmpEvents;
  });
}