简体   繁体   English

如何使用集合引用更新Firestore中的文档?

[英]How to update document in firestore using collection reference?

I'm having a problem in updating my document using collection reference in firestore android. 我在使用Firestore Android中的集合引用更新文档时遇到问题。 It is auto-generated id so I don't even know what is the id of my document. 它是自动生成的ID,所以我什至不知道我的文档ID是什么。

Save the id when retrieving the data eg 检索数据时保存ID,例如

db.collection("cities")
    .whereEqualTo("capital", true)
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    City city = document.toObject(City.class);

                    city.setId(document.getId()); //This is what you are looking for

                    ...

                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

You can that when updating 您可以在更新时

city.setName("LA"); //edit object
db.collection("cities").document(city.id).set(city); //save object

PS I have not run this code - could have syntax errors, careful not to copy paste. PS我没有运行此代码-可能有语法错误,请注意不要复制粘贴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firestore如何从文档参考包含特定查询路径的集合中查询数据 - Firestore how to query data from collection where document reference contains specific query path 在将数据添加到Firestore中的集合之前,获取文档的参考 - Get the reference of the document before adding the data to collection in Firestore 如何在 Firestore 的不同子集合中简化我的代码添加/更新文档? - How to simplify my code add/update Document in different sub-Collection in Firestore? 如何更新 Firestore 集合中的所有文档? - How to update all documents in collection of Firestore? 检查文档是否存在于 Firestore 集合中 - Checking if a document exists in a Firestore collection 如何自动检测 Firebase Firestore 集合中的新文档? - How to automatically detect new document in collection of Firebase Firestore? 如何使用Android在Cloud Firestore中查询特定文档后的集合? - How to query a Collection after a specific Document in Cloud Firestore with Android? 如何查询 Firestore 子集合并获取 spacyfyi 文档 ID? - How to query on Firestore sub collection and get spacyfyi document id? 带对象的Firestore文档包含参考 - Firestore Document with Object containing Reference 在 Firestore 中批量写入后如何获取更新的文档参考? - How to get the updated Document reference after batch write in Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM