Firebase Querying with 'order by' and 'where' returning null - flutter

var x = FirebaseFirestore.instance
.collection('Workouts')
.doc(FirebaseAuth.instance.currentUser?.uid)
.collection('allWorkouts')
.orderBy("Date Added", descending: true)
.where("Name", isEqualTo: workout.name)
.limit(limit)
.snapshots();
I have attached the code I am using to query data from my Firestore database. I have a field called 'Date Added' with a space. I have also added the respective index in firebase, with 'Date Added' and 'Name', both being ascending. I get an error below
Is there something I am doing wrong with the query, possibly due to the space in between the 'Date' and 'Added'?
Picture of the DB below where you can see that there are entries.

Try moving the Where clause before the OrderBy clause, like this:
var x = FirebaseFirestore.instance
.collection('Workouts')
.doc(FirebaseAuth.instance.currentUser?.uid)
.collection('allWorkouts')
.where("Name", isEqualTo: workout.name)
.orderBy("Date Added", descending: true)
.limit(limit)
.snapshots();
Also, you mentioned that you created a composite index with Date Added and Name fields both ascending, but you are ordering Date Added as descending, try fixing that index as attached in image

Related

FirebaseException: Not able to query a collectionGroup

I am trying to do a pretty simple query in firebase for a collectionGroup. I only want to get all the products that are of the type "Restuarant". The code is below:
QuerySnapshot res = await FirebaseFirestore.instance
.collectionGroup('products')
.where("type", isEqualTo: "Restuarant")
.get();
It keeps throwing a FirebaseException as below:
I have added an exception in the Firebase indexes. It is a single field index.
What is the issue here? Why is this Exception occurring?
So I was able to get it working by adding a composite index. After adding the composite index, I was able to perform orderBy query too, using the rating field.
QuerySnapshot res = await FirebaseFirestore.instance
.collectionGroup('products')
.where("type", isEqualTo: "Restuarant")
.orderBy("rating", descending: true)
.get();

How can I filter from the map in the document?

How can I filter from the map in the document?
my query function;
ref
.where('tag_id', isEqualTo: item)
.where('likes', arrayContains: {
Get.find<AuthController>().getCurrentUser!.uid: true
})
.orderBy('created_date', descending: true)
.orderBy('like_count', descending: true)
.orderBy('comment_count', descending: true)
.limit(max.value)
.get();
I want to filter on 'likes' map. I tried to make an example in the code below, but no data is coming.
map in document
returning null
Your likes is not an array, so array-contains won't work on it.
What you're looking for is :
const uid = Get.find<AuthController>().getCurrentUser!.uid;
ref
.where('tag_id', isEqualTo: item)
.where('likes.$uid', isEqualTo: true)
For more on this dot notation, see the Firebase documentation on updating fields in nested objects.

Trying to order Firestore results in query

I have a query that I would like to order by a start date and filter by end date.
return FirebaseFirestore.instance
.collection("content")
.where("active", isEqualTo: true)
.where("end", isGreaterThanOrEqualTo: DateTime.now())
.orderBy("start", descending: true)
.snapshots();
Error:
The initial orderBy() field '[[FieldPath([start]), true]][0][0]' has to be the same as the where() field parameter 'FieldPath([end])' when an inequality operator is invoked.
If you include a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field
https://firebase.google.com/docs/firestore/query-data/order-limit-data#limitations
So you should go with
return FirebaseFirestore.instance
.collection("content")
.where("active", isEqualTo: true)
.where("end", isGreaterThanOrEqualTo: DateTime.now())
.orderBy("end", descending: true)
.orderBy("start", descending:true)
.snapshots();
You are getting the following error:
The initial orderBy() field '[[FieldPath([start]), true]][0][0]' has to be the same as the where() field parameter 'FieldPath([end])' when an inequality operator is invoked.
Because there is no way you can get data from Firestore without first sorting on the end field when using an inequality operator. So you need to reorder the results in your application code. So after calling:
.where("end", isGreaterThanOrEqualTo: DateTime.now())
It's mandatory to call:
.orderBy("end", descending:false)
So your query should look like this:
return FirebaseFirestore.instance
.collection("content")
.where("active", isEqualTo: true)
.where("end", isGreaterThanOrEqualTo: DateTime.now())
.orderBy("end", descending:false)
.orderBy("start", descending:true)
.snapshots();
Don't also forget to create an index in order to make this query work.

How to set a not equal to a firestore query

I have this query
querySnapshot = await Firestore.instance
.collection('sellerPost')
.where("expireTime" , isGreaterThan: DateTime.now())
.where("category", isEqualTo: _selectedCategory)
.where("tags",arrayContains:_selectedTag )
.orderBy('expireTime',descending: true)
.limit(10)
.getDocuments();
I would like to query documents with given categories and tags as specified in the where clause, but in. a situation where the user has not selected a tag or category yet, i would like to query everything.
E.g in my mind i though this would work
querySnapshot = await Firestore.instance
.collection('sellerPost')
.where("expireTime" , isGreaterThan: DateTime.now())
.where("category", isEqualTo: "*")
.where("tags",arrayContains:"*" )
.orderBy('expireTime',descending: true)
.limit(10)
.getDocuments();
Is there a way to query with wild cards this way?

Collection Group query isn't returning any data flutter

I have set composite index and rules in console but still I'm unable to get any data. Below is snippet of my service for query.
_db.collectionGroup("entries")
.where("instructors", arrayContains: _id.getUser.mailID)
.where("branch", isEqualTo: _id.getBranch.name)
.where("date", isEqualTo: date)
.snapshots();