How to get specific document id Firestore?

I was trying to get document id for updating a data, but instead it update all data. How to get document id Firestore?

My Firestore Database

Code :

firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot documentSnapshot : task.getResult()) {
                String getDocumentID = documentSnapshot.getId();

                firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").document(getDocumentID).update("inspectorName", inspectorName, "marketLocation", marketLocation).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(StartCounting.this, "Document updated", Toast.LENGTH_SHORT).show();

                            finish();
                            overridePendingTransition(0, 0);
                            startActivity(getIntent());
                            overridePendingTransition(0, 0);
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(StartCounting.this, "Error : " + e.toString(), Toast.LENGTH_LONG).show();

                        progressUpdated.dismiss();
                    }
                });
            }
        }
    }
});

Upvotes: 2

Views: 4036

Answers (1)

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

Use set method with SetOptions.merge() to update only required field of document, but if you want to update only one document than don't use update query inside for loop.

firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot documentSnapshot : task.getResult()) {
                String getDocumentID = documentSnapshot.getId();
 Map<String, Object> mapUser = new HashMap<>();
        mapUser.put("inspectorName", inspectorName);
         mapUser.put("marketLocation", marketLocation);

                firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").document(getDocumentID).set(mapUser, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(StartCounting.this, "Document updated", Toast.LENGTH_SHORT).show();

                            finish();
                            overridePendingTransition(0, 0);
                            startActivity(getIntent());
                            overridePendingTransition(0, 0);
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(StartCounting.this, "Error : " + e.toString(), Toast.LENGTH_LONG).show();

                        progressUpdated.dismiss();
                    }
                });
            }
        }
    }
});

Upvotes: 2

Related Questions