Sunny Rahi
Sunny Rahi

Reputation: 99

Is it possible to create sub Collection like Firebase FireStore in MongoDB atlas?

Can we create sub Collection inside a mongoDB document as we can create in firebase firestore?

Upvotes: 1

Views: 1159

Answers (2)

Indra
Indra

Reputation: 521

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.

Upvotes: 1

Danny Varod
Danny Varod

Reputation: 18068

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.

Upvotes: 1

Related Questions