3

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 the best practice? What would you do?

Community
  • 1
  • 1
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • possible duplicate of [MongoDB Schema Design - Real-time Chat](http://stackoverflow.com/questions/2936598/mongodb-schema-design-real-time-chat) –  Apr 29 '11 at 02:54
  • Reasking the same question over and over again does not deserve any upvotes. –  Apr 29 '11 at 02:56

2 Answers2

0

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.

SethO
  • 2,703
  • 5
  • 28
  • 38
0

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

Community
  • 1
  • 1
  • Blackmoon this isn't an answer. It's a comment. And is disagree strongly about all mongodb schema design questions being the same. There are a dozen wrong ways to implement a schema in mongodb for every right way. I've rarely seen an answer on SO that gives a good answer to these questions. – reach4thelasers Jun 23 '12 at 09:01