【问题标题】:How to check whether a particular field exist in a particular Firestore document?如何检查特定 Firestore 文档中是否存在特定字段?
【发布时间】:2018-10-17 17:00:17
【问题描述】:

假设我有一份文件。我知道它的ID。现在我想知道该文档中是否存在特定字段。如果没有,我想创建它。有可能吗?

PS:我不想用我自己的字段名称并将该值设置为null

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    如何检查特定的字段是否存在于特定的 Firestore 文档中?

    要解决这个问题,您可以像这样简单地检查无效性:

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    DocumentReference docIdRef = rootRef.collection("yourCollection").document("yourDocumentId");
    docIdRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    if (document.get("yourField") != null) {
                        Log.d(TAG, "your field exist");
                    } else {
                        Log.d(TAG, "your field does not exist");
                        //Create the filed
                    }
                }
            }
        }
    });
    

    要向现有文档添加新的特定字段,请参阅我在此 post 中的回答。

    【讨论】:

      猜你喜欢
      • 2020-06-08
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      相关资源
      最近更新 更多