How would I design this schema in MongoDB?
Let's take an example of a chat room.
Should I create 2 collections: Room
and Messages
, and store the room details (title, description) separately from Messages (body/date/author)? The Messages collection would have a field called "Room" that links to the ObjectId of Room.
OR
Should I create 1 collection, called Room. And then inside Room, there is an array of messages?
What is开发者_如何学JAVA the best practice? What would you do?
I would lean toward your first choice. Aside from the fact that 16MB may be too small (I've seen some pretty busy chat rooms in my days), storing the messages separately allows for some greater flexibility on your part. The room doesn't even need to know what messages are associated with it - just create it once and query for messages by your Room
id as needed.
Schema design questions have been ask numerous times here. Please research since the solutions are always the same - you just have to think for a moment and apply them to your own usecase:
MongoDB Schema Design - Real-time Chat
Mongodb schema design
MongoDB Schema Design - Many small documents or fewer large documents?
It really should not be that hard replacing X in the answer with Y of your own problem.
In addition: the standard documentation applies as well (and is pretty much explicit on your problem):
http://www.mongodb.org/display/DOCS/Schema+Design
精彩评论