Android Firestore 查询对象数组中的特定值 [英] Android Firestore querying particular value in Array of Objects

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

问题描述

这是我的firestore数据库结构:

This is my structure of the firestore database:

预期结果:获取所有jobs,其中experience数组的lang值为Swift".

Expected result: to get all the jobs, where in the experience array, the lang value is "Swift".

因此,我应该得到前 2 个文件.第三个文档没有Swift"经验.

So as per this I should get first 2 documents. 3rd document does not have experience "Swift".

Query jobs = db.collection("Jobs").whereArrayContains("experience.lang","Swift");
jobs.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
               //Always the queryDocumentSnapshots size is 0
            }
        });

尝试了大部分答案,但没有一个成功.有没有办法查询这种结构中的数据?文档仅适用于普通数组.不适用于自定义对象数组.

Tried most of the answers but none worked out. Is there any way to query data in this structure? The docs only available for normal array. Not available for array of custom object.

推荐答案

使用您当前的文档结构,无法执行您想要的查询.Firestore 不允许查询列表字段中对象的单个字段.

With your current document structure, it's not possible to perform the query you want. Firestore does not allow queries for individual fields of objects in list fields.

您需要做的是在您的文档中创建一个可查询的附加字段.例如,您可以创建一个作为文档一部分的字符串语言列表的列表字段.有了这个,您可以使用数组包含查询来查找至少提到过一次语言的文档.

What you would have to do is create an additional field in your document that is queryable. For example, you could create a list field with only the list of string languages that are part of the document. With this, you could use an array-contains query to find the documents where a language is mentioned at least once.

对于屏幕截图中显示的文档,您将有一个名为languages"的列表字段,其值为 ["Swift", "Kotlin"].

For the document shown in your screenshot, you would have a list field called "languages" with values ["Swift", "Kotlin"].

这篇关于Android Firestore 查询对象数组中的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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