1

I am having issues with the code below, this statement is not returning sorted docs according to the sellingPrice.

databaseReference
            .collection("products")
            .where("tags", arrayContains: tag)
            .where("totalUnit", isGreaterThan: 0)
            .orderBy("totalUnit")
            .orderBy("sellingPrice", descending: isDesc)
            .limit(limit)
            .get()

Maybe it's the problem with the orderBy("totalUnit") statement because after removing it, the query is returning the docs in sorted manner.


I checked, and I think the order is sorted according to totalUnit, but I want the list to be sorted according to the sellingPrice.


And I cannot remove the orderBy("totalUnit") statement, because of the limitations of firestore, I need the statement because of the one above it.

For example, this below isn't exactly what I am getting but it's enough to make you understand the problem I've been facing,

Sample Data -

Products---->

Product1 - { "tags" :[a], "totalUnit": 2, "sellingPrice": 1000}
Product2 - { "tags" :[a], "totalUnit": 0, "sellingPrice": 700}
Product3 - { "tags" :[a], "totalUnit": 0, "sellingPrice": 2000}
Product4 - { "tags" :[a], "totalUnit": 3, "sellingPrice": 2000}
Product5 - { "tags" :[a], "totalUnit": 2, "sellingPrice": 500}

So that Function should only return,

If in ascending order - Product5, Product1, Product4

If in descending order - Product4, Product1, Product5

Mayur Agarwal
  • 1,448
  • 1
  • 14
  • 30
  • 1
    It's hard to help with "not working". Can you edit your question to include the actual problem you're experiencing, instead of just the code? For example: can you show/print the values that are not in the right order and then show us *that* code and its output? – Frank van Puffelen Aug 21 '21 at 14:32
  • Thanks @FrankvanPuffelen, I've updated the problem statement and also checked that after removing the two statements related to totalUnit (that weren't that necessary), it managed to solve the problem. But the problem is I don't want to get the data of the product that is out of stock, i.e. totalUnit =0. Kindly help me out. – Mayur Agarwal Aug 21 '21 at 16:17
  • @FrankvanPuffelen Is there any limit to how many queries in a single statement we can make to firebase firestore, like above I've used 2 where, 2 orderBy? – Mayur Agarwal Aug 21 '21 at 16:22
  • There are many documented [limits to Firestore queries](https://firebase.google.com/docs/firestore/query-data/queries#query_limitations). One thing to always check is whether you have an index for the query. If not, the SDK will log a very explicit error message, with a direct link to create the index. – Frank van Puffelen Aug 21 '21 at 16:56
  • Yes, I received an error, but then I also created an index for the query. Then there wasn't any error. I was receiving the data, but not in sorted manner. – Mayur Agarwal Aug 21 '21 at 17:05
  • @FrankvanPuffelen please help me out, I've updated the question. I checked, and I think the order is sorted according to totalUnit (first orderBy query), but I want the list to be sorted according to the sellingPrice (second orderBy query). – Mayur Agarwal Aug 23 '21 at 17:49
  • It seems like you re-asked the question with additional information here: https://stackoverflow.com/questions/68897423/how-to-sort-using-multiple-orderby-queries-in-firebase-firestore. In the future, please provide such information in your orignal question (so here) instead of opening a new question for the same problem. – Frank van Puffelen Aug 23 '21 at 20:12

1 Answers1

0

You used multiple times order by. For real-time database, you don't allow to use multiple times order by. For more, you go with this link link and you can also go with this link

Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38