如何获取特定的文档ID Firestore?

时间:2019-09-25 07:46:07

标签: java android firebase google-cloud-firestore

我试图获取用于更新数据的文档ID,但它会更新所有数据。如何获取文件ID Firestore?

My Firestore Database

代码:

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();
                    }
                });
            }
        }
    }
});

1 个答案:

答案 0 :(得分:1)

使用set方法与SetOptions.merge()一起仅更新文档的必填字段,但是如果只想更新一个文档,则不要在for循环中使用update查询。

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();
                    }
                });
            }
        }
    }
});