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屋!