Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取Firestore的特定文档id?_Java_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Java 如何获取Firestore的特定文档id?

Java 如何获取Firestore的特定文档id?,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我试图获取用于更新数据的文档id,但它更新了所有数据。如何获取Firestore的文档id 代码: firebaseFirestore.collection("Users").document(currentUser.getUid()).collection("Documents").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void

我试图获取用于更新数据的文档id,但它更新了所有数据。如何获取Firestore的文档id

代码:

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();
                    }
                });
            }
        }
    }
});
firebaseFirestore.collection(“Users”).document(currentUser.getUid()).collection(“Documents”).get().addOnCompleteListener(新OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(DocumentSnapshot DocumentSnapshot:task.getResult()){
字符串getDocumentID=documentSnapshot.getId();
firebaseFirestore.collection(“Users”).document(currentUser.getUid()).collection(“Documents”).document(getDocumentID)。更新(“inspectorName”,inspectorName,“marketLocation”,marketLocation)。addOnCompleteListener(新OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Toast.makeText(StartCounting.this,“文档更新”,Toast.LENGTH_SHORT.show();
完成();
覆盖转换(0,0);
startActivity(getIntent());
覆盖转换(0,0);
}
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
Toast.makeText(StartCounting.this,“错误:+e.toString(),Toast.LENGTH\u LONG).show();
progressUpdated.disclose();
}
});
}
}
}
});

使用set方法和SetOptions.merge()只更新文档的必填字段,但如果只更新一个文档,则不要在for循环中使用update query

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();
                    }
                });
            }
        }
    }
});
firebaseFirestore.collection(“Users”).document(currentUser.getUid()).collection(“Documents”).get().addOnCompleteListener(新OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(DocumentSnapshot DocumentSnapshot:task.getResult()){
字符串getDocumentID=documentSnapshot.getId();
Map mapUser=新建HashMap();
mapUser.put(“inspectorName”,inspectorName);
mapUser.put(“marketLocation”,marketLocation);
firebaseFirestore.collection(“Users”).document(currentUser.getUid()).collection(“Documents”).document(getDocumentID).set(mapUser,SetOptions.merge()).addOnCompleteListener(新OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Toast.makeText(StartCounting.this,“文档更新”,Toast.LENGTH_SHORT.show();
完成();
覆盖转换(0,0);
startActivity(getIntent());
覆盖转换(0,0);
}
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
Toast.makeText(StartCounting.this,“错误:+e.toString(),Toast.LENGTH\u LONG).show();
progressUpdated.disclose();
}
});
}
}
}
});

上述问题将非常棘手。查看代码后,我了解到您希望更新字段,但像往常一样,您需要查询中的
where
。所以,您可以帮助查询您的更新的常用内容吗。e、 g.
其中id=1
。请用要求更新问题请编辑问题以更具体地说明代码所做的与您期望的不同。您使用什么标准来确定所需文档的id?谢谢您的回答兄弟,我想再次询问。当我在firestore中有很多数据时,我想更新一个需要单个id的数据。我想问我如何获得单个id?当我更新数据时,您的代码实际上更改了所有数据。@Mr.Robot然后不获取整个收集数据,只获取您要更新的文档。