Reputation: 189
I'm making a site with meteorjs that has users and each user can make a list that is viewable and shareable by everyone. My question is about making an efficient mongodb schema.
Should I have a collection for users and a separate collection for posts and tie both of those together?
Or should I have just one collection that has users, and under each user there's a property that holds the posts?
Or is there another way to do this?
I've looked up various schema designs, but I'm confused on how to work a user's collection in with a 'posts' collection.
Any documentation, answers, or sense of direction would be greatly appreciatted.
Upvotes: 1
Views: 1925
Reputation: 7680
First of all, don't create your own users collection, use the accounts packages.
Generally, a document in a document database is a set of data that will be displayed at the same time (not always true, but the idea is to construct a page from as few documents as possible). For your use case, I would have a posts
collection which has a userId
field and a comments
array, each of which also has a userId
field. You could also store a subset of the user data in each post/comment - especially if that data can not change.
Note that this is a fundamentally different approach than with a SQL database, where we always want to normalize our data and join records as needed. In a document database, it's better to denormalize as much data as we can to optimize performance.
Upvotes: 3
Reputation:
This is going to be your best resource for everything mongoDB related (http://docs.mongodb.org/ecosystem/use-cases/)
The comments use case is the closest to what you have in mind.
Upvotes: 1