4

我正在检查名为的布尔字段是否attending存在,但是我不知道该怎么做。

我可以使用诸如.child().exists()或类似的功能吗?

firebaseFirestore.collection("Events")
    .document(ID)
    .collection("Users")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if(task.isSuccessful()) {
                for(QueryDocumentSnapshot document: task.getResult()){
                    attending = document.getBoolean("attending");
                }
            }
        }
    });
4

4 回答 4

3

您现在所做的是正确的 - 您必须阅读文档并检查快照以查看该字段是否存在。没有更短的方法可以做到这一点。

于 2019-10-01T08:09:37.803 回答
2

您可以执行以下操作:

  if(task.isSuccessful()){
    for(QueryDocumentSnapshot document: task.getResult()){
       if (document.exists()) {
          if(document.getBoolean("attending") != null){
             Log.d(TAG, "attending field exists");
          }
        }
      }
  }

文档

public boolean exists ()

如果此快照中存在文档,则返回 true。

于 2019-10-01T08:13:23.923 回答
0

这是实现它的方法,或者您可能已经解决了,这对于任何寻找解决方案的人来说都是如此。

根据文档:

CollectionReference citiesRef = db.collection("cities");

Query query = citiesRef.whereNotEqualTo("capital", false);

此查询返回每个城市文档,其中 capital 字段的值不是 false 或 null。这包括首都字段值等于 true 或除 null 之外的任何非布尔值的城市文档。

欲了解更多信息https ://cloud.google.com/firestore/docs/query-data/order-limit-data#java_5

于 2022-02-08T13:51:46.843 回答
-1
You can check firestore document exists using this,

CollectionReference mobileRef = db.collection("mobiles");
 await mobileRef.doc(id))
              .get().then((mobileDoc) async {
            if(mobileDoc.exists){
            print("Exists");
            }
        });
于 2021-07-21T14:05:07.430 回答