I have a collection in firestore where each document contains an array of contacts and I want to query those documents where any contact's email id is a certain value.
I came across whereArrayContains() filter at https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Query#whereArrayContains(java.lang.String,%20java.lang.Object), with the following description:-
public Query whereArrayContains (String field, Object value)
Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.A Query can have only one whereArrayContains() filter.
Can the value
in the above method point to a field inside an object for an array of objects?
Also, the phrase the value must be an array
is a little confusing given the method parameter is also called value
. I am sure the documentation means that the field
should be present in the document and its value should be an array and that the array should contain the value
parameter.
According to the documentation, the field point to the array and value means the String you want to check is present in array or not.
The data is added as:-
You cannot query fields of objects in arrays in a Firestore query. The array-contains query will instead compare with the objects in an array.
This refers to the value of the field you are trying to query against. Better phrasing for this would be
If you are trying to filter for a user ID or something similar, consider adding a second array to the object you are querying, then adding the IDs as strings to that array:
then query that array instead: