代码之家  ›  专栏  ›  技术社区  ›  Paradox

FireBase Cloud FireStore查询,其中EqualTo供参考

  •  3
  • Paradox  · 技术社区  · 7 年前

    我试图创建一个查询,它只选择引用等于给定引用的文档,使用Java进行Android开发。匹配的文档包含路径“/users/someuser”的引用。我正在创建这样的引用:

    DocumentReference ref = mDatabase.document("users/someUser");
    

    我也尝试过:

    DocumentReference ref = mDatabase.document("/users/someUser");
    

    然后查询:

    Query query = mDatabase.collection("myCollection").whereEqualTo("refField", ref).limit(10); 
    

    但是,当我运行查询并检查 task.isSuccessful() 在onComplete方法中,它不会通过,即它不起作用,而当我移除 .whereEqualTo() ,它通过,任务的结果不为空。我怎样才能正确使用 .WhereEqualTo()。 是否要检查包含特定引用的所有文档?

    与我的查询匹配的文档示例如下:

    /myCollection/GDpojS5koac2C7YlIqxS 其中包含字段:
    refField: /users/someUser (类型引用的值)

    一个与我的查询不匹配的文档示例是:

    /myCollection/J5ZcVAMYU1nI5XZmh6Bv 其中包含字段:
    refField: /users/wrongUser (类型引用的值)

    2 回复  |  直到 7 年前
        1
  •  0
  •   Luciano    7 年前

    我认为您需要添加一个get()方法来运行查询并添加一个onCompletionListener。

    这样的方法应该有效:

    mDatabase.collection("myCollection")
      .whereEqualTo("refField", ref)
      .limit(10)
      .get()
      .addOnCompleteListener({task ->
                             if(task.isSuccessful){
                                val result = task.result                 
                            })
    

    上面的例子是在KOTLIN中,但我想Java中有类似的例子。

        2
  •  0
  •   Abdul Aziz    7 年前

    您不必担心文档,如果您基于字段创建一个查询,那么所有文档都将返回到“querysnapshot”对象中。 例如,

    CollectionReference collectionReference = db.collection(FIRESTORE_USERS);
                    DocumentReference documentReference = collectionReference.document(userID);
                    CollectionReference notificationCollection = documentReference.collection(FIRESTORE_NOTIFICATIONS);
    
        notificationCollection.whereEqualTo(USER_TYPE, userType)
                            .whereGreaterThanOrEqualTo(SEND_AT, calendar.getTime())
                            .get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot documentSnapshots) {
                            List<DocumentSnapshot> snapshotsList = documentSnapshots.getDocuments();
                            ArrayList<NotificationCollections> notificationCollectionsArrayList = new ArrayList<>();
                            for (DocumentSnapshot snapshot : snapshotsList) {
                           // each document having that particular field based on query
        }
        }});
    

    在上面的示例中,我将获取所有与特定用户ID匹配并且时间大于或等于提供的时间的文档(在您的情况下,不会使用时间)。

    我希望这有助于…

    快乐编码:)

    推荐文章