Query
I made this query to search the documents based on doorNumber and merchantId which should be present in the merchants list in the document field and also prepared indexing but it's not working and showing that there are 0 customers.
final customersRef = FirebaseFirestore.instance
.collection('customerDB')
.where('merchants', arrayContains: merchantId)
.where('address.doorNumber', isGreaterThanOrEqualTo: searchKey)
.where('address.doorNumber', isLessThan: searchKey + 'z')
.orderBy('address.doorNumber')
.orderBy('name', descending: true)
.limit(limit);
Firebase data model
Changed z to ~ then it works but we can only search by prefix.
final customersRef = FirebaseFirestore.instance
.collection('customerDB')
.where('merchants', arrayContains: merchantId)
.where('address.doorNumber', isGreaterThanOrEqualTo: searchKey)
.where('address.doorNumber', isLessThan: searchKey + '~')
.orderBy('address.doorNumber')
.orderBy('name', descending: true)
.limit(limit);
Related
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
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.
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.
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?
I have a search screen in my app and i want to make sure you can search for every value in a firestore document. There are always 4 Keys in one document: title, author, genre and code.
getBookbyTitel(query) async{
return await Firestore.instance
.collection("book")
.where("titel", isEqualTo: query)
.getDocuments();
}
but with this code, I am only able to search for the title. how can I search for the three other keys a well?
Thanks
getBookbyTitel(query) async{
return await Firestore.instance
.collection("book")
.where("titel", isEqualTo: query).where("author", isEqualTo: query2).where("genre", isEqualTo: query3).
where("code", isEqualTo: query4).getDocuments();
}
Firestore has the advantage of having indexes complex and simple.
getBookbyTitel(query,query2,query3) async{
return await Firestore.instance
.collection("book")
.where("genre", isEqualTo: query)
.where("title", isEqualTo: query2)
.where("code", isEqualTo: query3)
.getDocuments();
There is no actual limit to how many .where() you can use, however this is what is called a "complex query" and as such the first time will fail because firestore needs to index first.
So, first run the code above, in the console an error message will pop up with a url that will send you to your firebase project and then on its own it will start indexing so the next time you run that code it will do it lightning fast.
However there is a downside, for each .where() you use, an additonal indexation will be needed for it to work properly. Have in mind that the order is important too, if you query in order [title,author,genre,code] you should always do it this way, otherwise it will detect it as a completely different query and will ask you to index again.
Finally there is also the issue that for example a more specific query is not the same as a query with less attributes. i.e:
getBookbyTitel(query,query2,query3) async{
return await Firestore.instance
.collection("book")
.where("genre", isEqualTo: query)
.where("title", isEqualTo: query2)
.where("code", isEqualTo: query3)
.getDocuments();
If you have this query, and then you try this instead:
getBookbyTitel(query,query2,query3) async{
return await Firestore.instance
.collection("book")
.where("genre", isEqualTo: query)
.where("title", isEqualTo: query2)
.getDocuments();
It won't work, it will ask you for another index.