java - Firestore : Query all the elements in array in android

标签 java android firebase google-cloud-firestore

我有一个像

这样的数据结构
 /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

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

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

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

最佳答案

official documentation 中:

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

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

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,而不是一个数组

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

更好的方法是考虑这种替代数据库结构,其中每个电话号码都是映射中的键并且所有值都为真:

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

2018 年 8 月 13 日编辑:

根据有关 array membership 的更新文档,现在可以使用 whereArrayContains() 方法根据数组值过滤数据。一个简单的例子是:

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.

关于java - Firestore : Query all the elements in array in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001490/

相关文章:

java - 在泛型中使用 super 和 extends 的有用示例?

Android Marshmallow 6.0,通过 Intent 打开图库说没有应用程序可以执行该操作

c# - 如何在 .Net C# Windows 窗体中将文件上传到 Firebase 存储?

javascript - 是否可以使用 Firebase Cloud Messaging 从 JS 进行双向消息传递?

java - 为什么此代码输出 "0"?

java - 如何使用 StAX 将元素添加到现有 XML 文件?

android - Caused by : java. lang.AbstractMethodError: Kotlin中的抽象方法

android - 如何在android中将数字转换为货币格式

java - FirebaseRecyclerAdapter 强制我实现我不想要/不需要的方法

java - 如何创建一个由用户输入定义长度的静态数组?