Firestore:查询android中数组中的所有元素 [英] Firestore : Query all the elements in array in android

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

问题描述

我有一个数据结构,如

 /Split       // Collection
   {authid}   // Document
      /SentIvitation //Collection
          {auto-id}  //Documnet
            amount    //array
             0:10
             1:10
             2:10
             Phonenumbers//array
               0:987654321
               1:123456789
               2:234567890

我需要查询电话号码中的所有元素,我必须检查这些电话号码是否已经存在于另一个集合中。
数据结构的下一个路径是

I need to query all the elements in Phone number and I have to check whether these phone numbers is already exist in another collection. The next path of my data structure is

/deyaUsers   //Collection
  {authid}.   //Documnet
      Name: "abc"
      Email: "abc@gmail.com"
      Phonenumber:987654321

在没有任何索引的情况下可以在firestore中使用吗?

Is it possible in firestore without any indexing?

推荐答案

官方文档:


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

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 = documentSnapshot.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getKey().equals("Phonenumbers")) {
        Log.d("TAG", entry.getValue().toString());
    }
}

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

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

只有在使用多个查询数据库时才需要使用索引属性。但不是你的情况。

You need to use indexes only when you query your database using more then one property. But is not your case.

如果你考虑这种替代数据库结构,其中每个电话号码是地图中的关键并且所有值都为真,那么更好的方法是:

A better approach will be if you consider this alternative database structure, where each phone number is the key in a map and all values are true:

Phonenumbers: {
    "987654321": true,
    "123456789": true,
    "234567890": true
}

编辑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.

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

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