Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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

Hi all,

Still not clear how to do an "orderBy" or a "where" filter in the collection query strings. I'm setting up a service in my app like so:

dbCountries: Observable<any[]>;
public getDBcountries() {
if (!this.dbCountries) {
const readCollection = collection(this.firestore, countryCollectName.collectName);
this.dbCountries = collectionData(readCollection, { idField: 'id' }).pipe(
shareReplay(1)
) as Observable<any[]>;
}
return this.dbCountries;
}

Anyone having any luck?

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: 2 comments 1 reply

Comment options

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 someone!

You must be logged in to vote
1 reply
Comment options

Thanks, here is a version I got working for making my country list to sort:

 // Make a country list service available across all pages
 dbCountries: Observable<any[]>;
 public getDBcountries() {
 if (!this.dbCountries) {
 const readCollection = collection(this.firestore, countryCollectName.collectName);
 const readQuery = query(readCollection, orderBy('countrySimple', 'asc')); // asc or desc
 this.dbCountries = collectionData(readQuery).pipe(
 shareReplay(1)
 ) as Observable<any[]>;
 } // can get the document ids using: readQuery, { idField: 'id' }
 return this.dbCountries;
 }
Answer selected by firepebble
Comment options

Thanks for this discussion. This example really should be part of the documentation.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /