且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firestore:在android中查询数组中的所有元素

更新时间:2023-02-05 15:55:29

官方文档:

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

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

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

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

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

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

电话号码:{987654321":对,123456789":对,234567890":真的}

编辑 2018 年 8 月 13 日:

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

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

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

I have a data structure like

 /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

Is it possible in firestore without any indexing?

As in the official documentation:

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

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
}

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.