node.js - Mongo DB - sub-collections? -
i'm new mongodb , using nodejs , native node mongodb driver. having doubts implementation. hope can me: have "schema", db.pages holds config each section of 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, define if have posts collection and, in node, when load page, check if page.posts
defined and, if so, load appropriate collection.
maybe i'm wrong, looking relational thing so, idea, put content of blogposts
directly value prop posts
of pages
collection, 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 think makes more sense, in nosql environment, might wrong. problem having using latter configuration, can't seem make nodejs treat posts
field mongo collection.
i hope makes sense and, if so, need know is:
- am right or wrong think second way right way?
- how can use node read 'sub-collection' collection can apply cursor , use
find()
,sort()
,limit()
,skip()
, etc, on it...
first approach better. second approach has multiple drawbacks:
- each document has size limit of 16mb. hit limit , not able add more blog posts
- you cannot query , fetch individual blog posts disk in 1 big document
- if apply principle, put comments in same pages document, further complicating , loose lot of flexibility
Comments
Post a Comment