如何在Android上使用whereArrayContains()过滤器查询Firestore集合中包含对象数组的文档? [英] How to query documents containing array of objects in Firestore collection using whereArrayContains() filter on Android?

查看:104
本文介绍了如何在Android上使用whereArrayContains()过滤器查询Firestore集合中包含对象数组的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firestore中有一个集合,其中每个文档都包含一个联系人数组,并且我想查询那些联系人的电子邮件ID是某个值的文档.

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.

我在

I came across whereArrayContains() filter at https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Query#whereArrayContains(java.lang.String,%20java.lang.Object), with the following description:-

公共查询whereArray包含(字符串字段,对象值)
创建并返回一个新的查询,其中包含文档必须包含的其他过滤器 包含指定的字段,该值必须是一个数组,并且 数组必须包含提供的值.

public Query whereArrayContains (String field, Object value)
Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.

一个查询只能有一个whereArrayContains()过滤器.

A Query can have only one whereArrayContains() filter.

以上方法中的value能否指向对象内部的对象数组?

Can the value in the above method point to a field inside an object for an array of objects?

此外,由于方法参数也称为value,因此短语the value must be an array有点令人困惑.我确信文档意味着field应该出现在文档中,并且其值应该是一个数组,并且该数组应该包含value参数.

Also, the phrase the value must be an array is a little confusing given the method parameter is also called value. I am sure the documentation means that the field should be present in the document and its value should be an array and that the array should contain the value parameter.

推荐答案

您无法在Firestore查询中查询数组中对象的字段.相反,包含数组的查询将与数组中的对象进行比较.

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

使用附加的过滤器创建并返回一个新的查询,该过滤器中的文档必须包含指定的字段,指定字段的必须是一个数组,并且该数组必须包含提供的值./p>

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.

如果您尝试过滤用户ID或类似内容,请考虑向要查询的对象添加第二个数组,然后将ID作为字符串添加到该数组:

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"]
}

然后查询该数组:

someRef.whereArrayContains("userIds", someId);

这篇关于如何在Android上使用whereArrayContains()过滤器查询Firestore集合中包含对象数组的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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