如何使用 React Native Firestore 以编程方式创建子集合

How to create subcollections programmatically with React Native Firestore

我希望使用 React Native Firebase 创建子集合来创建这样的 firestore 结构:

groups
   ->group
      ->scheduledQuestionnaires
         ->questionnaire (fields: id, type and timestamp)
users
   ->user
      ->groups
         (fields: id and name)

我已经创建了集合 groupsusers,但是如何以编程方式为每个组创建子集合 scheduledQuestionnaires

我可以很好地将带有字段的文档添加到集合中,但我似乎找不到创建(子)集合的方法。我是走错了路还是错过了什么?


编辑 添加到 monsty 的答案中。这是我想做的代码片段:

 firestore()
    .collection('groups')
    .doc('group1/scheduledQuestionnaires/questionnaire1')
    .set({
       id: '1',
       type: 'type',
       timestamp: 'timestamp',
    });

您不需要为每个集合创建子集合,因为 Firebase 会处理它。

如果您尝试将文档保存到“/groups/group/scheduledQuestionnaires/hello/world/my_document”,Firebase 将创建所需的所有子集合(在本例中为:hello 和 world)。