Dan Jhay :
I have a collection named Reports in my Firestore database, I've already done adding documents inside it. But my problem now is I want to add a Document with a SubCollection inside Reports, any Idea?
public CollectionReference Ref = firestore.collection("Reports");
Reports reports = new Reports(date, cash, total, discount, quantity, sum, employee);
Ref.add(reports);
Alex Mamo :
Reports
cannot be a document, it can only be a collection. So assuming you have a database structure that look like this:
Firestore-root
|
--- Reports (collection)
|
--- reportId (document)
|
--- // report details
To add a new collection under the reportId
document which in term contains other documents, please use the following line of code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference ref = rootRef
.collection("Reports").document(reportId)
.collection("NewCollection").document(newCollectionId);
ref.add(reports);
And your new sturcture will look like this:
Firestore-root
|
--- Reports (collection)
|
--- reportId (document)
|
--- // report details
|
--- NewCollection (collection)
|
--- newCollectionId (document)
|
---// newCollectionId details