Can we create sub Collection inside a mongoDB document as we can create in firebase firestore?
-
I'm not 100% sure what you meant by this, but perhaps my answer will help. – Danny Varod Dec 15 '21 at 16:29
-
1Have you checked this? [Is mongodb sub documents equivalent to Firestore subcollections?](https://stackoverflow.com/q/68574830/13130697) – Dharmaraj Dec 15 '21 at 17:27
2 Answers
MongoDB's logical architecture is:
Cluster
- Database
-- Collection
--- Document
You can have multiple databases, each with multiple collections, each with multiple documents, each with what ever you want to put in your documents.
Documents can have complex fields like objects/dictionaries and lists/arrays.
You can index on inner fields, not only on the top level ones.

- 17,324
- 5
- 69
- 111
You can't add a subcollection to a document in Mongodb like you can do in Firestore. In Firestore, when you read a document, the subcollections of the document are not read which allows subcollection of a document to be as large as needed.
MongoDb documents can contain fields which are objects, dictionaries or arrays and allows nesting of data to any level just like Firestore but does not allow subcollection.
However you can model your data in MongoDb in a way that mimics subcollections for each document. There are many ways to do it. One possible way is to use object keys to establish parent/child relationship between documents just like in SQL databases. All you are looking for is a one-to-many relationship between entities with the convenience of loading related objects with ease. You can do that quite easily in MongoDb. Plus MongoDb has a superior way of querying and projecting data using aggregation pipelines.

- 471
- 3
- 5