我已经阅读了Firestore文档,但还没有找到一个例子,其中有类似的内容。
collection
|--document
|--{auto-generated-id}
|--property1:value1
|--property2:value2
|--peoperty3:value3
我经常看到的是:
collection
|--{auto-generated-id}
|--property1:value1
|--property2:value2
|--peoperty3:value3
在前者中,我无法在文档上调用
add()
-(生成唯一ID)。但是,这可以在集合中完成,如上面的后一个草图所示。因此,我的问题是:
Firestore是否有办法在创建文档后帮助自动生成ID
IE
我如何实现这样的目标:
db.collection("collection_name").document("document_name").add(object)
最佳答案
如果您使用的是CollectionReference的add()方法,则意味着:
如果要获取生成的文档标识并将其用于引用中,请使用DocumentReference的set()方法:
就像下面的代码行一样:
String id = db.collection("collection_name").document().getId();
db.collection("collection_name").document(id).set(object);
关于android - 自动为文档生成ID,而不是在Firestore中生成ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52900318/