Angry Beaver
Angry Beaver

Reputation: 465

Angular 6 - AnguarFire - query a collection using multiple document fields

I use AngularFire & Cloud Firestore in my project and I need to make a query with multiple conditions(where) to a collection.

I tried to use this code but it's ignoring the second condition.

 this.itemCollection = this.afs.collection<Item>('items', ref => {

 return ref
         .where('size', '==', 'large')
         .where('brand', '==', 'some-brand')
 })
 this.items = this.itemCollection.valueChanges();

What am I doing wrong, how do I apply multiple conditions in AngularFire?

Upvotes: 1

Views: 5008

Answers (2)

Ayman Nedjmeddine
Ayman Nedjmeddine

Reputation: 12679

Queries on different fields using multiple where clauses are invalid [1][2]. This is not true => multiple where queries are supported by firebase when the query type is the same, eg "==" && "==". 3 only where you mix types eg "==" && "=>" will firebase reject it.

However, check this response to another questions on SO: Query based on multiple where clauses in Firebase.

Upvotes: 0

Other Barry
Other Barry

Reputation: 23

Your code needs to be:

this.itemCollection = this.afs.collection<Item>('items', ref => ref.where('size', '==', 
'large').where('brand', '==', 'some-brand'))
return this.items = this.itemCollection.valueChanges();

Upvotes: 2

Related Questions