Skip to content

How to add an OrderBy or Where to a collectionData query #3047

Answered by jjmhalew
firepebble asked this question in Q&A
Discussion options

You must be logged in to vote

I had the same issue and finally found the fix myself!

You will have to create one or more constraints, which is a where() for example.
Then you use a query, in which you pass the collection you want to query and the queryConstraints

Example in my code where I used 2 constraints to get orders from a specific date:

    const ordersRef = collection(this.firestore, this.ORDER_COLLECTION_NAME);
    const startDateConstraint = where('date', '>=', date1);
    const endDateConstraint = where('date', '<', date2);
    const ordersByTodayQuery = query(ordersRef, startDateConstraint, endDateConstraint);
    return collectionData(ordersByTodayQuery) as Observable<IOrder[]>;

I hope this will help som…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@firepebble
Comment options

Answer selected by firepebble
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants