Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo DB - sub-collections?

I'm really new to MongoDb and am using it with nodeJS and the native node mongodb driver. I am having doubts about implementation. I hope you can help me: I have this "schema", where db.pages holds the config for each section of my website:

db.pages = [
   {name: 'contacts', settings:{...}},
   {name: 'blog', settings:{...}, posts: "blogPosts"},
   {name: 'media', settings: {...}, posts: "mediaPosts"}
]

db.blogPosts = [
  {title: 'post1', date: '2011-10-22', author:'me', content: '...'},
  {title: 'post2', date: '2011-11-22', author:'me', content: '...'},
  {...............................................................}
];

What I'm doing here is, in each page, I define if I have a posts collection and, in node, when I load the page, I check if page.posts is defined and, if so, load the appropriate collection.

Maybe I'm wrong, but this is looking too much as a relational thing so, my idea, would be to put the content of blogPosts directly as the value for the prop posts of the pages collection, like so:

db.pages = [
       {name: 'contacts', settings:{...}},
       { name: 'blog', 
         settings:{...}, 
         posts: [
            {title: 'post1', date: '2011-10-22', author:'me', content: '...'},
            {title: 'post2', date: '2011-11-22', author:'me', content: '...'},
            {...............................................................}
         ]
       },
       {name: 'media', settings: {...}, posts: [...]}
    ]

I really think this makes much more sense, in a NoSQL environment, but I might be wrong. The problem I am having is that using the latter configuration, I can't seem to make nodejs treat the posts field as a mongo collection.

I hope this makes any sense to you and, if so, what I need to know is:

  1. Am I right or wrong to think the second way is the right way?
  2. How can I use node to read that 'sub-collection' as collection so I can apply a cursor and use find(), sort(), limit(), skip(), etc, on it...
like image 366
André Alçada Padez Avatar asked Mar 09 '12 12:03

André Alçada Padez


People also ask

Does MongoDB have Subcollections?

One aspect of organizing in MongoDB that we see under-utilized is the use of Subcollections. Subcollections sound slightly intimidating and a lot of developers didn't even know they existed, and for good reason.

How do I query embedded files in MongoDB?

Accessing embedded/nested documents – In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.

How can you implement 1 to many relationships in MongoDB?

In MongoDB, one-to-one, one-to-many, and many-to-many relations can be implemented in two ways: Using embedded documents. Using the reference of documents of another collection.

How do you link collections in MongoDB?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.


1 Answers

First approach is better. Second approach has multiple drawbacks:

  1. Each document has a size limit of 16MB. You will hit that limit and will not be able to add more blog posts
  2. You cannot query and fetch individual blog posts from the disk as its all in one big document
  3. if you apply this principle, then you will put all the comments also in the same pages document, further complicating it and will loose lot of flexibility
like image 200
Samarth Bhargava Avatar answered Sep 19 '22 21:09

Samarth Bhargava