FIRESTORE-将subCollection插入文档中

时间:2018-08-14 14:56:56

标签: java android firebase google-cloud-firestore

我的Firestore数据库中有一个名为Reports的集合,我已经在其中添加了文档。但是我现在的问题是我想在Reports中添加一个带有SubCollection的文档,任何想法吗?

public CollectionReference Ref = firestore.collection("Reports");
Reports reports = new Reports(date, cash, total, discount, quantity, sum, employee);
Ref.add(reports);

1 个答案:

答案 0 :(得分:1)

Reports不能是文档,只能是集合。因此,假设您具有如下所示的数据库结构:

Firestore-root
    |
    --- Reports (collection)
          |
          --- reportId (document)
                |
                --- // report details

要在包含其他文档的reportId文档下添加新集合,请使用以下代码行:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference ref = rootRef
    .collection("Reports").document(reportId)
    .collection("NewCollection").document(newCollectionId);
ref.add(reports);

您的新结构将如下所示:

Firestore-root
    |
    --- Reports (collection)
          |
          --- reportId (document)
                |
                --- // report details
                |
                --- NewCollection (collection)
                       |
                       --- newCollectionId (document)
                              |
                              ---//  newCollectionId details