1

I'm making a social media app like instagram, but trying to filter out the user's own posts so they don't see it on their feed. I'm trying to do it like below, but it causes the app to crash. When I change it to .whereEqualTo, it works just fine and only shows posts by the users. As far as I know, the two should work exactly the same, with the obvious exception of it being equal to vs not, so why does one work but the other doesn't?

Does not work

 Query query = firestoreDb.collection("posts")
.whereNotEqualTo("user.username", username)

Shows only posts by users

Query query = firestoreDb.collection("posts")
.whereEqualTo("user.username", username)

The error I get is

You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'user.username' and so you must also have 'user.username' as your first orderBy() field, but your first orderBy() is currently on field 'creationTime' instead

but I don't want to order by username.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
John
  • 11
  • 2
  • Please edit the question to show the specific documents that you expect to match in each case. We need to be able to see your data to know how these queries will work. If there is an error message, you should show that as well. – Doug Stevenson Dec 28 '20 at 01:23

1 Answers1

3

If you want to use an inequality filter (such as whereNotEqualTo) in your query, you are obliged to order the results by the field on that filter. That is a hard requirement of Firestore, due to the way it's organized. The only way to work around that is to reorder the query results in your app code - you will not be able to coerce the query to do what you want.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441