Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore: orderBy combined with where causes error "Operation was rejected"

I am looking at the Firebase Cloud Firestore documentation for orderBy. When I try to execute this

var facultyQuery = facultyRef.where("department", "==", "Core Teacher").orderBy('bb_last_name', 'desc');

I get the error:

Error: Firestore: Operation was rejected because the system is not in a state required for the operation`s execution. (firestore/failed-precondition).

Both of these simpler cases work just fine:

var facultyQuery = facultyRef.orderBy('bb_last_name', 'asc');
var facultyQuery = facultyRef.where("department", "==", "Core Teacher");

But when I combine the where and the orderBy, something I have done before with other Firestore collections, it fails.

Here is a sample record: enter image description here

like image 908
AdamG Avatar asked Dec 11 '17 01:12

AdamG


People also ask

How do I list all collections in firestore?

You can do the following: const admin = require("firebase-admin"); const db = admin. firestore(); db. listCollections() .

When should you not use firestore?

If you have a very time-sensitive app, like online auctions where you can't have unpredictable delays (no one likes to lose because of something that we don't control), you shouldn't really use Firestore. You will have a hard time hiding the hiccups and you will only anger your users.

How many documents can firestore handle?

20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.

What is a Subcollection in firebase?

A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms. class roomA.


2 Answers

Tip for anyone that needs it,

If you've forgotten to create an index and are getting the above error, run adb logcat and then attempt to load the data again - it usually gives you a URL link which will very kindly create the required index for you.

like image 73
Caphson Avatar answered Oct 11 '22 19:10

Caphson


I encountered this same issue, and Frank van Puffelen's comment fixed it for me. You need to create a composite index for "department" and "bb_last_name". Since "department" uses the equality operator, it doesn't matter whether it is ascending or descending in your index.

like image 31
Connor M Avatar answered Oct 11 '22 18:10

Connor M