[英]Edit Firestore collectionGroup query documents
I am wondering if it is possible to update the fields of a document from a collectionGroup query.我想知道是否可以从 collectionGroup 查询中更新文档的字段。 I have set up my query as below and I want to.update() a single field in the document the query returns.我已经将我的查询设置如下,我想在查询返回的文档中更新()单个字段。
Query query = db.collectionGroup("orders").whereEqualTo("restaurantid",fAuth.getUid()).whereEqualTo(docId,getId());
query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (!queryDocumentSnapshots.isEmpty()){
Order order = queryDocumentSnapshots.getDocuments().get(0).toObject(Order.class);
//can i update the document here?
}else
{
Log.d("STATUS ERROR", "QUERY IS EMPTY");
}
}
});
To write to a document in Firestore you need a DocumentReference
to it.要写入 Firestore 中的文档,您需要一个DocumentReference
。 Your queryDocumentSnapshots.getDocuments().get(0)
gives you a DocumentSnapshot
, which has a getReference
method .你的queryDocumentSnapshots.getDocuments().get(0)
给你一个DocumentSnapshot
,它有一个getReference
方法。 So:所以:
Query query = db.collectionGroup("orders").whereEqualTo("restaurantid",fAuth.getUid()).whereEqualTo(docId,getId());
query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (!queryDocumentSnapshots.isEmpty()){
DocumentSnapshot snapshot = queryDocumentSnapshots.getDocuments().get(0);
// Order order = snapshot.toObject(Order.class);
DocumentReference ref = snapshot.getReference();
ref.update("fieldName", "value");
} else {
Log.d("STATUS ERROR", "QUERY IS EMPTY");
}
}
});
You can update value of any document with its reference and update
method.您可以使用其引用和update
方法更新任何文档的值。
DocumentReference washingtonRef = db.collection("cities").document("DC");
washingtonRef.update(key, value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.