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

后端 未结 2 1140
野趣味
野趣味 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:28

    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.

    You can use the array_contains operator to filter based on array values. For example:

    CollectionReference citiesRef = db.collection("cities");
    citiesRef.whereArrayContains("regions", "west_coast");
    

    This query returns every city document where the regions field is an array that contains west_coast. If the array has multiple instances of the value you query on, the document is included in the results only once.

    The data is added as:-

    CollectionReference cities = db.collection("cities");
    
    Map data1 = new HashMap<>();
    data1.put("name", "San Francisco");
    data1.put("state", "CA");
    data1.put("country", "USA");
    data1.put("capital", false);
    data1.put("population", 860000);
    data1.put("regions", Arrays.asList("west_coast", "norcal"));
    cities.document("SF").set(data1);
    

提交回复
热议问题