Home > Enterprise >  Firestore: Inequality filter property and first sort order must be the same. Failing where condition
Firestore: Inequality filter property and first sort order must be the same. Failing where condition

Time:05-02

I have a collection of documents like this:

{
   uid: <UNIQUE_USER_ID>,
   lastLoggedIn: <TIMESTAMP_IN_NUMBER>,
   ...
}

when I try to fetch the data using:

Server.db
    .collection(firebasePaths.funfuse_verified_users)
    .where('uid', 'not-in', notToIncludeAccounts)
    .orderBy('lastLoggedIn', 'desc')
    .startAt(startAt)
    .limit(limit)

I get an error saying:

Error: 3 INVALID_ARGUMENT: inequality filter property and first sort order must be the same: uid 
and lastLoggedIn

Is this a known limitation of firebase or am I doing something wrong ? I want to query all the users which aren't in an array using not-in and then sort those results based on lastLoggedIn timestamp activity.

CodePudding user response:

As the error message says, in order to be able to use not-in you must first order on the same field:

Server.db
    .collection(firebasePaths.funfuse_verified_users)
    .orderBy('uid')
    .where('uid', 'not-in', notToIncludeAccounts)
    .orderBy('lastLoggedIn', 'desc')
    .startAt(startAt)
    .limit(limit)

This also means that the results will be order first on uid and only then (so if there are multiple results with the same uid value) on lastLoggedIn. If you need them in another order, you will have to resort them in your application code.

  • Related