Firestore Whereequalto, Orderby and Limit(1) Not Working

Firestore whereEqualTo, orderBy and limit(1) not working

This query will not work unless you create an index for it. This can be done, by creating it manually in your Firebase Console or if you are using Android Studio, you'll find in your logcat a message that sounds like this:

FAILED_PRECONDITION: The query requires an index. You can create it here: ...

You can simply click on that link or copy and paste the URL into a web browser and your index will be created automatically.

Firestore Query whereEqualTo along with whereGreaterThan not working

You cannot put multiple conditions with different fields in a query without a composite index.

You can find more info in the official documentation:

https://firebase.google.com/docs/firestore/query-data/index-overview

https://firebase.google.com/docs/firestore/query-data/indexing

BTW, firebase creates a link to create these indexes, usually, it can be found in console (logcat in android case)

Perform whereEqualTo with orderBy in firestore

You might need an index on "Type". An error message with an URL should be printed in your logcat as well. If you can find that, just click on that URL. Otherwise, try to create one manually..

https://firebase.google.com/docs/firestore/query-data/indexing

How to use whereEqualTo() and orderBy() in a Firebase Firestore query?

The document field for the filter is called "fulfilled", but you are using "isFulfilled" to filter it. That won't work at all. You must use the same name of the actual field.

database
.collection("Orders")
.whereEqualTo("Donor", email)
.whereEqualTo("fulfilled", false)
.orderBy("timestamp", Query.Direction.DESCENDING).get()

Error is coming while get query in Android Studio Firestore

The error message is quite explicit about the error. You are using both "whereGreaterThan()" and "whereLessThan()" on the "postDate" field, but you aren't first ordering the results based on that field, hence that error. Please remember, that the order of the method calls in Firestore is very important. To solve this, please change your query to:

query = database.collection("CustomerViews/Data/Post")
.orderBy("postDate", Query.Direction.DESCENDING)
.whereGreaterThanOrEqualTo("postDate", startDate)
.whereLessThanOrEqualTo("postDate", endDate)
.orderBy("postViews", Query.Direction.DESCENDING)

And right after that, simply create the corresponding index. That's it.


Edit:

According to your first comment:

By using your code data is coming but "postViews" descending is not happening. Only postDate descending is coming.

Yes, the results will be returned descending according to "postDate". And if two or more elements will have the same "postDate", then only those results will be after that ordered descending according to "postViews". This is how Firestore works.

According to your second comment:

I want the "postViews" in descending order in given date ranges.

Cloud Firestore queries can only sort or filter range on a single field. What you are trying to achieve it's not possible, since you are trying to filter on "postDate" and then order on "postViews".

In the official documentation, there is an example on how not to do it:

  • Range filter and first orderBy on different fields

     citiesRef.whereGreaterThan("population", 100000).orderBy("country"); //Invalid

According to your last comment:

I got the solution for my problem. I have added the below line before attaching the ArrayList to an adapter.

postArrayList.sortByDescending { it.PostViews  }

Indeed it will work if you download all documents on the client and to the filtering there, but it will cost you one document read for each document you download.

Why firestore multiple whereEqualTo are not working

You're querying over terminal but the actual key in your db is terminalId

Android firebase firestore how to initialize recyclerview

I'm not sure what is the problem. If the list is duplicated and you want the new one to be replaced for the old one just empty your current list (I guess you use userArrayList to show items in the recycler view) and add the new content.

What I mean is to re-initialize the list before adding new content if you don't want to duplicate content.



Related Topics



Leave a reply



Submit