在Firestore中查询结果的查询功能 [英] Query function for distinct result in firestore
问题描述
是否有任何查询功能可在firestore中获得不同的结果.
Is there any Query function to get the distinct result in firestore.
例如:
Query query = colRef.orderBy("title")
.startAt("R")
.limit(10);
这给了我所有带有"title"(以"R"开头)的文档,其中包含重复的内容:
This gives me all the documents with "title" starting with "R", which contain duplicates like this:
Recording
Running
Running
Running
如何获得这样的独特结果:
How can I get a distinct result like this:
Recording
Running
推荐答案
不幸的是,Cloud Firestore中没有 distinct()
方法.因为您要按 title
排序数据,这是每个孩子中的关键,所以这是可能的.要查询数据库并仅获取distnict元素,您需要提出另一种解决方案,即创建另一个名为 distinctTitles
的节点,您需要在其中添加所有这些标题.为了避免重复的元素(覆盖数据),首先您需要使用
Unfortunately there is no distinct()
method within Cloud Firestore. Because you are ordering your data by title
, which is a key within each child this is possible. To query your database and get only the distnict elements you need to come up with another solution which would be to create another node named distinctTitles
in which you need to add all those titles. To avoid duplicate elements (overriding data), first you need to check for uniqueness
using exists() method like this:
DocumentReference docRef = db.collection("distinctTitles").document("titleToCheck");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.exists()) {
//Do something
} else {
//Do something else
}
}
});
这篇关于在Firestore中查询结果的查询功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!