I'm starting working with collections groups in Firebase and I cannot make it work. I need to filter it by billable
leases:
users/portfolios/rents
Here I have a property which is a list of leases (not a collection, just a property) called leases
with some properties like billable
:
I have created a single-field index:
And also a composite index (not sure if I also need it):
This is my query using Flutter:
FirebaseFirestore.instance
.collectionGroup('rents')
.where('billable', isEqualTo: true)
.get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
print(doc);
}
});
The error I get is:
Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console.
It's not indexed already?
CodePudding user response:
For the query in your question you'll need a collection group index on the finished
field of the rents
collection group, which I don't see in any of the screenshots you shared.
From the screenshot of a document you linked in the comments here:
So billable
is a value in each item of the leases
array field. There is no way to query on such a single value in an array field, as the ==
operator checks whether the entire array field is the same as the value you specify, and the arrayContains
operator checks if any array item matches the entire value you specify.
The common workaround is to extra the value you want to query on to a top-level field, either an array field or a single boolean. For example, if you add a field hasAnyBillableLeases
to the rents
document, you can query on that.