我有一个像
这样的数据结构 /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 中使用?
最佳答案
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/