且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Firestore获取对象数组

更新时间:2023-11-30 23:07:40

按照有关阵列的官方文档


虽然Cloud Firestore可以存储数组, 但它不支持 查询数组成员或更新单个数组元素。

Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.

如果您只想获得整个论文数组,则需要迭代 Map ,如下所示:

If you only want to get the entire papers array you need to iterate over a Map like this:

Map<String, Object> map = document.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getKey().equals("papers")) {
        Log.d("TAG", entry.getValue().toString());
    }
}

但请注意,即使论文对象作为数组存储在数据库中, entry.getValue()返回 ArrayList ,而不是数组

But note, even if papers object is stored in the database as an array, entry.getValue() returns an ArrayList, not an array.

编辑2018年8月13日:

根据有关数组成员资格,现在可以使用 whereArrayContains()方法根据数组值过滤数据。一个简单的例子是:

According to the updated documentation regarding array membership, now it is possible to filter data based on array values using whereArrayContains() method. A simple example would be:

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




此查询返回每个城市文档,其中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.