简体   繁体   中英

How to retrieve a value from a sub-field of a map field in a document in Firestore DB?

I am planning to use the value of a sub-field of a map field in my document for other purposes but it seems that I cannot retrieve it. I have found an answer here on this website but the solution code to get the value is too much for me. I can use the solution code to get the value but if there is the simplest way to get it, kindly drop the answer here.

This is the screenshot of Firestore DB where I need to get is the Boolean value of deleted inside a map field with the UID as field name:

在此处输入图片说明

To get the value of the "deleted" fields that exists inside that Map object, please use the following lines of code:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference candidatesRef = db.collection("Candidates");
DocumentReference emailRef = candidatesRef.document("ravalera...@umak.edu.ph");
//                                                   ^ add entire address ^
emailRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Boolean> map = (Map<String, Boolean>) document.get("PADc...ayU2");
//                                                                             ^ add entire ID
                boolean deleted = map.get("deleted");
                Log.d(TAG, "deleted: " + deleted);
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

The result in the logcat will be:

deleted: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM