Is there any method to create sub-collections under a collection ?

117 views
Skip to first unread message

Praneeth Tvss.

unread,
Mar 6, 2018, 6:01:46 AM3/6/18
to mongodb-user
Hi Everyone,

AFAIK, at the database, if we want to store the data it follows a particular architecture like: 
- Create a database.
- Create a collection (data)

---database
       |
       |___ Collection(data)

But is there any method that supports the creation of sub-collections under the collection.
- Create a database,
- Create a collection,
- Create a sub-collection (data)

---database
      |
      |__ Collection
                |
                |__ Sub-collection(data).

I tried searching for the method which helps my requirement. But I didn't find anything.
Please help me to get a solution for this. If anyone didn't understand this Q please let me know.

Thank you.

Terry Brugger

unread,
Mar 6, 2018, 5:50:48 PM3/6/18
to mongodb-user
Praneeth,

I am pretty sure there is not: the database->collection->document hierarchy seems to be fairly fundamental to the API. Two approaches you can take would be to elevate the collection to its own database, or to treat the sub-collections as collections grouped by convention. For example, let's say you want a collection called colour with sub-collections red, green, and blue -- you could make color its own database with red, green, blue collections, or leave it in your existing database with color-red, color-green, and color-blue collections -- just note that when you use special characters in collection names you often need to wrap the collection in a lookup like dbname["color-red"] instead of dbname.color-red.

Hope that helps!
Terry

Praneeth Tvss.

unread,
Mar 7, 2018, 4:12:45 AM3/7/18
to mongodb-user
Thank you very much, Terry.

I am going with 2nd approach. I am going to create a collection with unique convention name.

Thank you.


Reply all
Reply to author
Forward
0 new messages