[英]How to add a new Firestore document with a nested collection in React?
How would I go about adding a new document in Firebase/Cloud Firestore with a nested collection?我将如何使用嵌套集合在 Firebase/Cloud Firestore 中添加新文档?
const createDocument = () => {
const documentName = prompt('Enter document name:');
if (documentName) {
db.collection('despacito').add({
name: documentName,
})
}
Any suggestions?有什么建议? Thank you for your time.
感谢您的时间。
Per the data model doc , Collections and documents are created implicitly in Cloud Firestore.根据数据模型 doc ,集合和文档是在 Cloud Firestore 中隐式创建的。 Simply assign data to a document within a collection.
只需将数据分配给集合中的文档。 If either the collection or document does not exist, Cloud Firestore creates it.
如果集合或文档不存在,Cloud Firestore 会创建它。
So no need to actually create a document with a nested collection.因此无需实际创建具有嵌套集合的文档。
Per the subcollections documentation to reference a new document in a subcollection you can reference it as:根据子集合文档引用子集合中的新文档,您可以将其引用为:
const messageRef = db.collection('rooms').doc('roomA').collection('messages').doc('message1');
As you are using the .add method , it should be something like:当您使用 .add 方法时,它应该类似于:
db.collection('despacito').doc('documentName').collection('subcollectionname').add({
name: nesteddocumentName,
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.