Conducting this answer after some others, I will repeat some of the things mentioned. Accept the first suitable answer, not this one.
However, there are a few things to consider. Consider these three questions:
- Will you always ask for all the comments every time you request a message?
- Do you want to request comments directly (for example, request comments for a specific user)?
- Will your system have relatively low usage?
If all questions can be answered yes, then you can embed an array of comments. In all other scenarios, you probably need a separate collection to store your comments.
First of all, you can actually update and delete comments in the safe concurrency mode (see updates using positional operators), but there are some things you cannot do, such as index based inserts.
The main problem with using built-in arrays for any large collection is the problem of switching to updating. MongoDB reserves a certain amount of indentation (see db.col.stats().paddingFactor
) per document to allow it to grow as needed. If you run out of this add-on (and this will often be the case in your case), he will have to move this ever-growing document to disk. This makes updates an order of magnitude slower and therefore a serious problem for high-bandwidth servers. A related but slightly less important issue is bandwidth. If you have no choice but to request the entire post with all its comments, even if you show only the first 10, you will spend a lot of traffic, which can be a problem for cloud environments (you can use the $ slice to avoid some of them) .
If you want to enable the built-in here, do the following basic operations:
Add a comment:
db.posts.update({_id:[POST ID]}, {$push:{comments:{commentId:"remon-923982", author:"Remon", text:"Hi!"}}})
Update comment:
db.posts.update({_id:[POST ID], 'comments.commentId':"remon-923982"}, {$set:{'comments.$.text':"Hello!"}})
Delete comment
db.posts.update({_id:[POST ID], 'comments.commentId':"remon-923982"}, {$pull:{comments:{commentId:"remon-923982"}}})
All of these concurrency methods are safe, because update criteria are part of a write (process) lock.
With all that, you probably want to get a dedicated collection for your comments, but this happens with the second choice. You can either store each comment in a highlighted document, or use comment codes, say, 20-30 comments each (described in detail here http://www.10gen.com/presentations/mongosf2011/schemascale ). This has advantages and disadvantages, so it is up to you to decide which approach is best for what you want to do. I would go for buckets if your comments for a post can exceed a couple of hundred due to the performance o (N) of the sk (N) cursor method, which you will need to swap. In all other cases, just go with the comments to the workflow. This is most flexible with requesting comments for other use cases.