Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore OrderBy and Where conflict

I have simple fetch data from my Firestore db, but i would like to paginate it with some orderring and where conditions. So i am trying to fetch data with some basic filters, but face error, in docs https://firebase.google.com/docs/firestore/query-data/order-limit-data described that only for range <, <=, >, >= should use orderBy and where for same field, but i need only full match (==)

node v8.12.0, express, firebase functions

 model.collection
        .orderBy("dateCreated", 'desc')//timeStamp
        .where('tenantId', '==', 'f8XnOVUKob5jZ29oM9u9')  
        .limit(10)
        .get()
        .then((snapshot) => {
          res.send(snapshot);
        }).catch((error) => res.send(error));

got next error

{
    "code": "failed-precondition",
    "name": "FirebaseError"
}

i have results only when use where or orderBy separetly but not in same time

like image 636
Simon Pasku Avatar asked Jun 15 '19 20:06

Simon Pasku


People also ask

How do you query collections in firestore under a certain path?

There is no way to get documents from different collections or sub-collections in a single query. Firestore doesn't support queries across different collections in one go unless we are using a collection group query.

How do I reorder documents in firebase?

By default, a query retrieves all documents that satisfy the query in ascending order by document ID. You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() . Note: An orderBy() clause also filters for existence of the given field.

Are firestore queries case sensitive?

Save this answer. Show activity on this post. Yes, queries are still case sensitive.


1 Answers

When working with compound queries, you need to create an index for your queries. Your query fails because you didn't create indexes for

dateCreated
tenantId

Your index tab should have something similar to the following, with your indexed fields.

index

like image 173
Junius L. Avatar answered Sep 24 '22 08:09

Junius L.