Firestore firebase Android search and update query

后端 未结 1 2037
迷失自我
迷失自我 2020-12-12 07:20

I am trying to update a field of existing document in my collection in FireBase Firestore. I want to search that document using one of its field. My collec

相关标签:
1条回答
  • 2020-12-12 07:36

    To solve this, please use the following lines of code:

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    CollectionReference complaintsRef = rootRef.collection("complaints");
    complaintsRef.whereEqualTo("id", 5).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Map<Object, String> map = new HashMap<>();
                    map.put("complainant_Name", "ABC");
                    complaintsRef.document(document.getId()).set(map, SetOptions.merge());
                }
            }
        }
    });
    

    After using this code, the property complainant_Name will hold the value of ABC.

    0 讨论(0)
提交回复
热议问题