如何从 Firestore 获取对象数组 [英] How to get an Array of Objects from Firestore

查看:19
本文介绍了如何从 Firestore 获取对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询作为地图数组的字段的正确方法是什么.

目前的结构是

Collection1文件1-papers: <---- 这是一个数组(0):-名称:abc-ID:123(1):-名称:xyz-ID:456

这是我的代码

DocumentReference docRef = db.collection("Collection1").document("Document1");docRef.get().addOnCompleteListener(new OnCompleteListener() {@覆盖public void onComplete(@NonNull Task task) {如果(任务.isSuccessful()){DocumentSnapshot 文档 = task.getResult();如果(文档!= null && document.exists()){//??我怎样才能取回论文}}});

基本上我是检索它并将其转换为 ArrayList> 然后循环遍历它以创建我的最终 ArrayList 吗?

或者它是如何工作的?

解决方案

根据 官方关于数组的文档:

<块引用>

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

如果您只想获取整个 papers 数组,则需要像这样迭代 Map:

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

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

编辑 2018 年 8 月 13 日:

根据关于阵列成员资格的更新文档,现在可以使用 whereArrayContains() 方法基于数​​组值过滤数据.一个简单的例子是:

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

<块引用>

此查询返回每个城市文档,其中区域字段是包含 west_coast 的数组.如果数组有您查询的值的多个实例,则该文档仅包含在结果中一次.

What would be the right way to query a field that is an array of maps.

Currently the structure is

Collection1
     Document1
         -papers:                <---- This is an array  
              (0): 
                 -Name:abc
                 -Id:123 
              (1): 
                 -Name:xyz
                 -Id:456

And this is my code

DocumentReference docRef = db.collection("Collection1").document("Document1");
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null && document.exists()) {
                       //?? how can I retrieve papers
                }
            }
        });

Basically do I retrieve it and cast it as an ArrayList> and then loop through it to create my final ArrayList ?

Or how does it work ?

解决方案

As per official documentation regarding arrays:

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

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());
    }
}

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

Edit 13 Aug 2018:

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");

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.

这篇关于如何从 Firestore 获取对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆