简体   繁体   English

如何在文档中创建集合?

[英]How to create a collection inside a document?

I am writing a Flutter application with Firebase.我正在使用 Firebase 编写 Flutter 应用程序。 When creating a user I am doing:创建用户时,我正在做:

Firestore.instance.collection('users').document(user.uid).setData({
        'name': name + ' ' + surname,
        'email': user.email,
        'registerDate' : DateTime.now(),
        'isEmailVerified': user.isEmailVerified,
        'location': location,
        'photoUrl': user.photoUrl,
        'votes' : {},

      });

I want the "votes" to be a collection and contain documents.我希望“投票”成为一个集合并包含文档。

How to create a collection inside a document?如何在文档中创建集合?

If you need a subcollection under your uid document that should contain votes, then use the following lines of code:如果您的uid文档下需要包含投票的子集合,请使用以下代码行:

Firestore.instance.collection('users').document(user.uid)
    .collection('votes').document(vote)
    .setData({/* ... */});

In which vote is an object that is added under votes subcollection.其中, vote是一个对象,它被添加到了votes子集合下。

If you want to add the collection right after you added the user to the users collection, then check when the setData() operation completes and right after that write the vote object to the above reference.如果你想添加之后添加的用户到收集users收集,再检查时, setData()操作完成,并且写了之后vote对象上面的参考。

One more thing to remember is that in Cloud Firestore documents and subcollections don't work like filesystem files and directories.要记住的另一件事是,在 Cloud Firestore 中,文档和子集合不像文件系统文件和目录那样工作。 Please also see my answer from the following post:另请参阅我在以下帖子中的回答:

Firestore document/subcollection is not existing Firestore 文档/子集合不存在

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM