【问题标题】:How to get specific document id Firestore?如何获取特定的文档 ID Firestore?
【发布时间】:2019-09-25 07:46:07
【问题描述】:

我试图获取文档 ID 以更新数据,但它更新了所有数据。如何获取文档 ID Firestore?

代码:

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

【问题讨论】:

  • 以上会比较有问题。查看您的代码后,我了解到您想要更新字段,但像往常一样,您需要 where 进行查询。因此,您能否帮助您了解可用于查询更新的常见内容。例如where id = 1。请根据要求更新问题
  • 请编辑问题,更具体地说明代码的作用与您的预期不同。
  • 你使用什么标准来确定你想要的文档的id?

标签: java android firebase google-cloud-firestore


【解决方案1】:

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

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

【讨论】:

  • 谢谢你的回答兄弟,我想再问一次。当我在 Firestore 中有大量数据时,我想更新需要单个 id 的数据。我想问我如何获得单身ID?当我更新数据时,您的代码实际上会更改所有数据。
  • @Mr.Robot 然后不要获取整个集合数据,只需获取您要更新的文档。
猜你喜欢
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 2023-03-03
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多