【问题标题】:Query function for distinct result in firestoreFirestore中不同结果的查询功能
【发布时间】:2017-12-18 05:52:22
【问题描述】:

是否有任何查询函数可以在 firestore 中获得不同的结果。

例如:

Query query = colRef.orderBy("title")
                        .startAt("R")
                        .limit(10);

这给了我所有以“R”开头的“title”的文档,其中包含这样的重复:

Recording
Running
Running
Running

我怎样才能得到这样的明显结果:

Recording
Running

【问题讨论】:

标签: android google-cloud-firestore


【解决方案1】:

很遗憾,Cloud Firestore 中没有 distinct() 方法。因为您通过title 对数据进行排序,这是每个孩子中的一个键,这是可能的。要查询数据库并仅获取 distnict 元素,您需要提出另一个解决方案,即创建另一个名为 distinctTitles 的节点,您需要在其中添加所有这些标题。为避免重复元素(覆盖数据),首先您需要使用exists() 方法检查uniqueness,如下所示:

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

【讨论】:

  • 您可以为此探索firebase功能,如果您将firebase用于多平台,实现此类功能可能是更好的选择。
猜你喜欢
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 2021-04-19
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多