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:
find()
, sort()
, limit()
, skip()
, etc, on it...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.
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.
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.
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.
First approach is better. Second approach has multiple drawbacks:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With