簡體   English   中英

如何檢查特定Firestore文檔中是否存在特定字段?

[英]How to check whether a particular field exist in a particular Firestore document?

可以說我有一個文件。 我知道它的ID 現在,我想知道該文檔中是否存在特定字段。 如果沒有,我想創建它。 可能嗎?

PS:我不想將字段名與我自己的字段一起放置,並將該值設置為null

如何檢查特定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
                }
            }
        }
    }
});

要將新的特定字段添加到現有文檔中,請參閱我在這篇文章中的回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM