I want to fetch the most liked posts of the last few days (3 days in this example).
Timestamp maxDaysAgoTimestamp =
Timestamp.fromDate(DateTime.now().subtract(Duration(days: 3)));
Query query = FirebaseFirestore.instance
.collection("posts")
.where("timestamp",
isGreaterThanOrEqualTo: maxDaysAgoTimestamp)
.orderBy("likes", descending: true)
.limit(10);
QuerySnapshot snapshot = await query.get();
I am trying to achieve this with the above query, but I am getting the following error.
Failed assertion: line 492 pos 13: 'conditionField == orders[0][0]':
The initial orderBy() field "[[FieldPath([likes]), true]][0][0]" has to be
the same as the where() field parameter "FieldPath([timestamp])" when an inequality
operator is invoked.
Appearently I would have to add an ordering on the timestamp
as the first ordering, but then it would order by timestamp
first and only consider the likes
ordering when the timestamp is the same which is useless.
So am I doing something wrong? Or do you have any other ideas I could implement this use case?
In my opinion this use case is not too special, so there should be a way to do it.