2

I have a firestore collection 'orders' whose documents has a timeStamp value. I want to filter documents of that collection based on timeStamp. For example, filter orders placed on July 1 ,2022. I pass date value got from Datepicker as DateTime. The query i formed is

_db.collection('orders')
        .where('driverId', isEqualTo: sp.getString('uid'))
        .where('timeStamp', isGreaterThanOrEqualTo: Timestamp.fromDate(pickedDate!))
        .get().then((querySnapshot){
      querySnapshot.docs.forEach((element) {
        orders.add(element.data());
      });
      print(orders.length);
    });

The problem is i'm getting orders from July 1 till today since i have given isGreaterThanOrEqualTo. But if i give isEqualTo,it returns nothing. Means it takes 01-07-2022 00:00:00 i guess. So what is the correct query to get orders on a selected date?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • hi Azhagappan Kathiresan . I am unable to filter the value . Could you please help me out? I am using this command to filter the activities from firebase collections. finalRef = finalRef .where('operatingStatus', isEqualTo: 'Open') .where('endDate', isGreaterThanOrEqualTo: Timestamp.fromDate( DateTime.parse('2022-02-06 00:00:00'))) .where('endDate', isLessThanOrEqualTo: Timestamp.fromDate( DateTime.parse('2022-12-30 00:00:00'))); – Rahul Kushwaha Nov 15 '22 at 09:59
  • I am getting empty value []. – Rahul Kushwaha Nov 15 '22 at 10:02

1 Answers1

5

Timestamp objects in Firestore are very specific points in time, with a microsecond precision. So if you want to match all timestamps for an entire day, that is a range of timestamp values.

The typical approach is to create a timestamp of one day later, and then add that to the second clause needed for the range:

_db.collection('orders')
    .where('driverId', isEqualTo: sp.getString('uid'))
    .where('timeStamp', isGreaterThanOrEqualTo: Timestamp.fromDate(pickedDate!))
    .where('timeStamp', isLessThan: timestampOfStartOfNextDay)
veli
  • 249
  • 1
  • 5
  • 14
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807