How to query documents containing array of objects in Firestore collection using whereArrayContains() filter on Android?

后端 未结 2 1143
野趣味
野趣味 2020-12-03 21:36

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.

2条回答
  •  臣服心动
    2020-12-03 22:41

    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.

    the value must be an array

    This refers to the value of the field you are trying to query against. Better phrasing for this would be

    Creates and returns a new Query with the additional filter that documents must contain the specified field, the value of the specified field must be an array, and that the array must contain the provided value.

    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:

    {
        "name": "test",
        "users": [someUserObject, someOtherUserObject],
        "userIds": ["someId", "someOtherId"]
    }
    

    then query that array instead:

    someRef.whereArrayContains("userIds", someId);
    

提交回复
热议问题