Home > Back-end >  How to order a query on multiple fields in firestore flutter
How to order a query on multiple fields in firestore flutter

Time:01-23

I want to order a query on multiple fields in Firestore using Flutter. I use FirebaseFirestore.instance.collection('sgees').orderBy('uid').orderBy('timestamp').get(). But I get the following error:

Listen for Query(target=Query(sgees order by uid, timestamp, __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/myfirebaseprojectname/firestore/indexes?create_composite=Ck9wcm9qZWN0cy9mbHV0dGVyLXNhbGFtLWFwcC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvc2dlZXMvaW5kZXhlcy9fEAEaBwoDdWlkEAEaDQoJdGltZXN0YW1wEAEaDAoIX19uYW1lX18QAQ
Unhandled Exception: [cloud_firestore/failed-precondition] The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/myfirebaseprojectname/firestore/indexes?create_composite=Ck9wcm9qZWN0cy9mbHV0dGVyLXNhbGFtLWFwcC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvc2dlZXMvaW5kZXhlcy9fEAEaBwoDdWlkEAEaDQoJdGltZXN0YW1wEAEaDAoIX19uYW1lX18QAQ

What is the solution? Thanks in advance.

CodePudding user response:

To order a query on multiple fields in Firestore using Flutter, you can chain multiple orderBy method calls together in the query. In your example, you are ordering the query by the 'uid' field first, and then by the 'timestamp' field. The get() method at the end of the query will execute the query and return the results.

However, it's important to note that you can only order a query by one field at a time. Therefore, chaining multiple orderBy calls will only order by the last one specified.

To order by multiple fields you can use .orderBy(fieldPath, directionStr), where fieldPath is the name of the field you want to order by, and directionStr is either 'asc' or 'desc'.

FirebaseFirestore.instance.collection('sgees')
  .orderBy('uid', 'desc')
  .orderBy('timestamp', 'asc')
  .get();

The error message you are seeing indicates that the query you are trying to perform requires an index to be created in Firestore.

When you query a collection in Firestore, the query can only be executed if it is able to be fulfilled by an index. An index is a data structure that allows Firestore to efficiently find the documents in a collection that match a particular query. By default, Firestore creates an index for each field in a collection, but if you want to perform a query that involves multiple fields, you need to create a composite index for those fields.

The error message includes a link to the Firebase console where you can create the composite index for your query. You need to go to the link and create the composite index for the fields 'uid' and 'timestamp' in the 'sgees' collection.

Once you have created the index, your query should work as expected.

Also, keep in mind that composite indexes are created at the collection level, so if you have multiple queries that need to use the same index, you only need to create it once.

  • Related