有没有办法从 Firestore 访问自动生成文档 ID?

2024-04-10

我可以知道有什么方法可以使用Java访问Firestore中的自动生成文档ID吗?因为我想通过我自己的 Android 应用程序向现有文档添加新字段和数据。我已经浏览了文档,但没有找到任何可以解决问题的内容。

Elaboration of problem: I try to obtain the auto-generate ID of each "Event" document for the purpose of adding/update new field "Save Event" for each "User" in Firestore. So that 1 user can save many "event" inside the firestore.Code that I try to figure out DB structure


为了使用您正在查找的文档 ID,首先需要将其存储在变量中。当您将文档添加到数据库并使用document()方法调用不传递任何参数,会生成一个唯一的 ID。要获取该 ID,您应该使用以下代码:

String docId = collRef.document().getId();
collRef.document(docId).set(obj);

其中“collRef”是一个引用,指向要在其中添加文档的集合,“obj”是要添加到数据库中的对象。

如果需要,您还可以将文档 ID 存储为对象的属性。

获得此 ID 后,您可以像这样执行更新:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference collRef = rootRef.collection("collName");
collRef.document(docId).update(map).addOnCompleteListener(/* ... /*);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法从 Firestore 访问自动生成文档 ID? 的相关文章

随机推荐