简体   繁体   English

如何在 React 中添加带有嵌套集合的新 Firestore 文档?

[英]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.

相关问题 如何在Firestore上的文档内部添加集合? 反应本机 - How to add a collection inside document on Firestore? React Native 反应 Firestore 查询嵌套集合 where 字段然后更新,或添加新文档 - react Firestore query nested collection where field then update, or add new doc 如何连续将对象添加到 Firestore 中的嵌套集合 - How to continuously add objects to nested collection in Firestore 反应 firebase 将新文档添加到集合 - react firebase add new document to collection Cloud Functions:如何将 Firestore 集合复制到新文档? - Cloud Functions: How to copy Firestore Collection to a new document? 如何使用模块 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合? - How do I get document and nested collection in a document in a Cloud Firestore collection using Version 9 of the Modular Web SDK? 如何使用 react 从集合中删除/编辑 Firestore 文档 - How delete/edit Firestore document from collection with react 如何将新文档批量添加到集合中? - How add new document to a collection in a batch? 文档内嵌套集合的 Firestore onSnapshot - Firestore onSnapshot of nested collection inside document 如何将嵌套属性保存在 MongoDB 的新集合中的新文档中? - How to get nested properties to be saved in a new document in a new collection in MongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM