skip to Main Content
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?

3

Answers


  1. Chosen as BEST ANSWER

    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;
      });
    }
    

  2. 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")
    )
    
    Login or Signup to reply.
  3.   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
        });
      });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search