Search code examples
firebaseangular5google-cloud-firestoreangularfire5

How to express nested fields in the where() method of AngularFirestore


I'm using the firestore where() method to test the equality of a nested field against another value. The nested field is in the document structure: Apartments/Apartment/property/address/locality_short

Here is how I am currently doing it (in the code below) but it's not returning any documents: //There import import { AngularFirestore} from 'angularfire2/firestore';

//The injection constructor(private afs: AngularFirestore){}

//The method which retrieves apartments based on whether //apartment.property.address.locality_short == search_object.locality_short

search(search_obj: Search):Observable{ return this.afs.collection('/Apartments', ref => ref.where(property,address,locality_short, '==', 'search_obj.Address.locality_short')).valueChanges() }


Solution

  • Solved! after trying out different ways of expressing the nested fieldPath, it turns out that using the dot '.' to separate the nesting levels works just fine and my other mistake was expressing the value in my where() method as a string instead of as an actual value. So the code below works and returns results based on the evaluation of a nested fieldPath against a value in the where() method of angularfirestore

    search(search_obj: Search):Observable{ return this.afs.collection('/Apartments', ref => ref .where(property.address.locality_short,'==',search_obj.Address.locality_short)) .valueChanges() }