Reputation: 653
I have a collection (groups) with documents that has multiple properties, one of them (members) is an array of Maps with the following structure:
members:[{"is_admin":true,"user":"users/someuserid"}]
When "members" was an array of references (to user documents) it was easy to find all the groups a user is member of:
db.collection("groups").whereField("members", arrayContains: db.document("users/"+UserDefaults.standard.string(forKey: "firebase_uid")!) ).getDocuments(){
I'm trying to achieve the same thing (get all the group documents a user is member of) but I can't find the right way to query the inside of the map property.
Upvotes: 3
Views: 3011
Reputation: 757
Seems like there has been an update sometime in firebase
now you can say :-
i come from dart so syntax will be different below
CollectionReference _collectionReference= FirebaseFirestore.instance.collection('groups');
String _searchingForThisUserID = users/someuserid; // which is the value u were looking for
QuerySnapshot _collectionSnapshot = await _collectionReference
.where('anotherFieldNameIfYouWant', isEqualTo: 'otherValueIfYouWant')
.where('members.user', isEqualTo: '_searchingForThisUserID ')
.get();
I guess you will get my point here regardless the syntax
you will see this members.user
how I could access a value of a map inside firebase array by this map's field name
Upvotes: 0
Reputation: 317798
You currently can't do this type of query with Firestore. You can only match the entire contents of some object in an array, not a specific field in an object in an array. If you want to match an array field containing exactly {"is_admin":true,"user":"users/someuserid"}
, you should be able to do that. You will have to provide the exact fields of all the fields of the object to search for.
If you want to just find documents with a particular string value, consider breaking that value out into its own field in order to make it possible to index and search.
Upvotes: 0