Firestore Firebase Android搜索和更新查询 [英] Firestore firebase Android search and update query
问题描述
我正在尝试更新 FireBase Firestore 中的集合中现有文档的字段.我想使用其字段之一来搜索该文档. 我的收藏名称是投诉", 我的文档的结构看起来像-
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 collection name is "complaints", My document's structure looks like --
complainant_Name: "XYZ"
complaint_Details : "some random details"
e_Mail: "xyz@gmail.com"
id: 5
phone_No: "1234567890"
我要搜索文档,如果文档的"id"字段为5,我想更新文档,说将"complainant_Name"字段更改为"ABC".如何在Java Android中为此查询编写代码?在 Firebase Firestore 中搜索后,我找不到任何能说明如何更新的代码源.
I want to search documents and if "id" field of a document is say 5, I want to update it say change "complainant_Name" field to "ABC". How to write the code for this query in java android? I couldn't find any code source which tells how to update after searching in Firebase Firestore.
推荐答案
要解决此问题,请使用以下代码行:
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());
}
}
}
});
使用此代码后,属性complainant_Name
将保存ABC
的值.
After using this code, the property complainant_Name
will hold the value of ABC
.
这篇关于Firestore Firebase Android搜索和更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!