使用Firebase时,如何检查特定文档中是否存在字段?

时间:2020-09-17 14:38:28

标签: java android firebase google-cloud-firestore

在Firebase Firestore中,我有一个Collection及其相应的文档,例如

PRODUCTS
        Document with AutoId
                            Fields:
                                   a
                                   b

我想检查该文档中是否存在该字段

Ex- if field a exists some code will execute if not exists some other code will execute
if(task.isSuccessful()){
   for(long x=0;x<(long)task.getResult().get("no_of_products");x++){
      if(task.getResult().get("productID_"+x)==null){
        continue;
      }else{
         //some code
       }
   }
}

这是检查字段是否存在于文档中的正确方法吗?

1 个答案:

答案 0 :(得分:1)

如果您只想检索该字段所在的文档,无论它具有什么值,都可以在查询中使用startAt条件:

db.collection("PRODUCTS")
  .orderBy("no_of_products")
  .startAt(null);