Firestore range queries on timestamp

14,684 views
Skip to first unread message

taulant mehmeti

unread,
Oct 3, 2019, 11:41:49 AM10/3/19
to Firebase Google Group
I'm raising this issue regarding range queries on timestamp. 

Description of the problem

 I'm querying documents by timestamp and it is returning an empty array. However it works when I use "==" ex:.where('date', '==',timestamp), and returns empty array when I use '>' or '<'.

I have tried to convert timestamp into date object and string also but no success.

Note: The date field in firestore is of type Timestamp.

I'm querying documents with a date greater than '2018-08-03' in the collection. Below is a picture of the collection of transactions (left) and the document(right) which should be part of the array of documents returned, because the date is greater than '2018-08-03'.


This is the code:

  const firstDay = new Date('2018-08-03');
  const timestamp1 = admin.firestore.Timestamp.fromDate(firstDay);
  const trans = [];
  const docRef = db.collection('Users').doc(uid).collection('transactions').where('item_id', '==', item_id)
    .where('date', '>', timestamp1);
  await docRef.get()
    .then((snapshot) => {
      snapshot.forEach((doc) => {
        trans.push({ transaction_id: doc.id, transaction: doc.data() });
      });
    })
    .catch(err => new Error('cannot get the documents', err));



Expected result: should be an array with transactions with a date greater than specified above.

Actual result: empty array.

Assumptions: Since its working for equality ==, I assumed that and < would work. Anything I'm missing here?

Logs: Below you can see logs after trying "==" (equal to timestamp1)

firstDay: Fri Aug 03 2018 02:00:00 GMT+0200 (Central European Summer Time)

timestamp1: Timestamp { _seconds: 1533254400, _nanoseconds: 0 }
```[
    {
        "transaction_id": "1qKbdNn1bWHr4lAog3D6Umq3xd4oMWt5zb6vR",
        "transaction": {
            "iso_currency_code": "USD",
            "amount": 500,
            "date": {
                "_seconds": 1533254400,
                "_nanoseconds": 0
            },
            "categories": [
                "Food and Drink",
                "Restaurants"
            ],
            "account_id": "Xeqyv7k5yrtj9V17Q6B8uP54WgZ6RgCdRB8jn",
            "category_id": "13005000",
            "payment_meta": {
                "payment_method": null,
                "payment_processor": null,
                "reason": null,
                "payer": null,
                "by_order_of": null,
                "reference_number": null,
                "payee": null,
                "ppd_id": null
            },
            "pending": false,
            "name": "KFC",
            "transaction_type": "place",
            "category": "Food and Drink",
            "location": {
                "lon": null,
                "lat": null,
                "address": null,
                "region": null,
                "city": null,
                "postal_code": null,
                "country": null,
                "store_number": null
            },
            "item_id": "zGRkpP3QkzI89zKyDjZ7FPDrXd5G3Bco5ENlR",
            "recurrent": 0,
            "institutionId": "ins_3",
            "account_owner": null,
            "transaction_id": "1qKbdNn1bWHr4lAog3D6Umq3xd4oMWt5zb6vR"
        }
    }
]





Kato Richardson

unread,
Oct 8, 2019, 8:58:53 PM10/8/19
to Firebase Google Group
Hello Taulant,

It took me a few minutes to work this out myself. Docs aren't exactly clear here. Looks like in JS it accepts Date objects.


☼, Kato


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/1ce0f9cf-c283-4654-9110-777a0b070ddd%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Doug Stevenson

unread,
Oct 9, 2019, 5:56:37 PM10/9/19
to Firebase Google Group
Your query includes "where('item_id', '==', item_id)".  Is that what you intended?  The date range won't work if the item_id is also not exactly the same in the document

Doug

Sergey Y.

unread,
Dec 4, 2019, 10:41:00 AM12/4/19
to Firebase Google Group
HI, 

I have a same issue. Did you manage to solve it? 

Regards,
Sergiy

Kato Richardson

unread,
Dec 6, 2019, 10:18:25 AM12/6/19
to Firebase Google Group
Hi Sergey,

THe example I linked is functional. You can pass in Date or Timestamp objects.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

Umair Ashraf

unread,
Sep 28, 2021, 9:20:26 AM9/28/21
to Firebase Google Group
For the field date did not work but I had my own field named start=Timestamp(...) and that worked. For future explorers!

Oh I also had to create a composite index as shown in the console with an absolute link.
Reply all
Reply to author
Forward
0 new messages