Firestore: Chaining the where and orderBy in JavaScript

902 views
Skip to first unread message

Gyen Abubakar

unread,
May 17, 2020, 12:23:56 PM5/17/20
to Firebase Google Group
Hello everyone!

I'm new to Firestore and I've run into an issue. 

I made a query in a "cafes" collection to fetch every document whose "city" property is equal to "London", and then order the fetched data by the "city" property:
dbRef.collection("cafes")
   
.where("city", "==", "London")
   
.orderBy("city").get().then(snapshot => {
     
// do something with snapshot
   
})

But then the code above yielded this error:

Screenshot (11).pngI've searched online but haven't found issue that is similar to this one. Any help will really help.

Michael Bleigh

unread,
May 17, 2020, 1:06:57 PM5/17/20
to Firebase Google Group
Ordering the data by the same field as an equality filter doesn't make sense: the returned results will only have one value for the field ("London") in your case, and so there is no order to them -- they're all the same!

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/51305246-6fc2-4c26-8090-235b61c9f023%40googlegroups.com.

Gyen Abubakar

unread,
May 17, 2020, 4:19:04 PM5/17/20
to Firebase Google Group
That makes sense. Thanks for your reply.

Kato Richardson

unread,
May 18, 2020, 11:36:16 AM5/18/20
to Firebase Google Group
Hello Gyen,

Keep in mind that you can add an additional field to order on though, if you want consistent ordering.

☼, Kato

On Sun, May 17, 2020 at 1:19 PM Gyen Abubakar <inde...@gmail.com> wrote:
That makes sense. Thanks for your reply.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Sam Stern

unread,
May 18, 2020, 12:53:38 PM5/18/20
to Firebase Google Group
It's also worth noting that all Firestore queries have an implicit "order by __name__" clause at the end which orders the documents by their IDs.  This means even with no orderBy clause your results will always be returned in the same order.

- Sam

Reply all
Reply to author
Forward
0 new messages