Reputation: 23
I'm trying to query Firestore documents by a Timestamp field (e.g. between a start and end date).
Timestamp field returns something like Timestamp(seconds=1599170400, nanoseconds=0)
.
I have a Unix timestamp (in seconds) in my app which I like to compare this to (using isLessThan:), but the value in Firestore apparently is inside this Timestamp object.
How would I go about comparing these two timestamps?
An alternative would be to query all documents and compare afterwards, but I would like to filter the documents in the query itself.
Upvotes: 2
Views: 2584
Reputation: 598901
To compare a Timestamp
in the database with a milliseconds-since-the-epoch value, you can create a Timestamp
object based on that value:
var timestamp = Timestamp.fromMillisecondsSinceEpoch(1599573193925);
Then you can use this in the query against the value in the database:
Firestore.instance
.collection('items')
.orderBy('Timestamp', descending: true).startAt([timestamp])
Or (my usual method):
Firestore.instance
.collection('items')
.where("Timestamp", isGreaterThanOrEqualTo: timestamp)
Upvotes: 7