I have a collection in cloud firestore that contains a lot of documents that have a timestamp value for when the document was created, along with some more information. I'm experiencing some very strange behavior, that I just can't wrap my head around.
What I'm trying to do is:
- Find the newest document based on timestamp value
- Find the oldest document that is newer than 1 hour ago, 24 hours ago, and 7 days ago.
I have these queries:
var snapshotNow = await Firestore.instance
.collection(stationName)
.orderBy('servertime', descending: true)
.limit(1)
.snapshots()
.first;
And to find 1 hour ago, and so on:
var dateTime = DateTime.now().subtract(duration);
And to retrieve the document from 1 hour ago:
var snapshotThen = await Firestore.instance
.collection(stationName)
.where('servertime', isGreaterThan: dateTime)
.orderBy('servertime', descending: false)
.limit(1)
.snapshots()
.first;
For some reason, these two very different queries retrieve the same document each time. That is: I get the newest document in the collection from both queries.
I tried to do a couple of things to find out what the problem is:
1) Not looking at the date, by removing the .where
in the query. This should make snapshotThen
become the oldest document in the collection. What I actually received was the newest document in the collection. This means that the same document was returned as the first document when sorting both ascending and descending.
2) Increasing the limit to more than 1 document. When increasing the limit from limit(1)
to limit(10)
. For the ascending-sort, this consistently returns the newest 10 documents (as expected). For the orderBy descending, something weird happens. I expect it return the oldest 10 documents from the collection. What it returns is one of two things; either 1) the 10 newest documents, sorted from oldest to newest, or 2) only the newest document (not a list of 10 documents, but a list of only 1 document).
Any help is greatly appreciated, I've been working on this for ages, and I just can't wrap my head around where the problem lies. What I find the most frustrating is that the behavior changes; sometimes I get only 1 document, sometimes I get 10 documents, all with the same query.