开发者

How to control the document insertion point in MongoDB?

开发者 https://www.devze.com 2023-02-21 05:02 出处:网络
Suppose I have blog posts table. I want to store all comments on a blog post together. If there is a new comment on a post after 1 year, I want it to be added to the last comment on the collection.

Suppose I have blog posts table. I want to store all comments on a blog post together. If there is a new comment on a post after 1 year, I want it to be added to the last comment on the collection. This is to ensure that when I read the comments of that post, I do a SEEK operation.

Otherwise, I would have to scan for the comments of that post if they are scattered. Plus sharding/partition might put comments of same post on different parts.

Comments
{
   {post_id: 1, cmt_id:1, usr: 'bob', text: 'Cool!'}
   {post_id: 1, cmt_id:2, usr: 'bob', text: 'Cool!'}
   {post_id: 2, cmt_id:3, usr: 'rob', text: 'Cool!'}
   {post_id: 2, cmt_id:4, usr: 'job', text: 'Cool!'}
}

If there is a new comment for post 1, then the collection I want:

Comments
{
   {post_id: 1, cmt_id:1, usr: 'bob', text: 'Cool!'}
   {post_id: 1, cmt_id:2, usr: 'bob', text: 'Cool!'}

   {post_id: 1, cmt_id:5, usr: 'bob', text: 'Cool!'} <-- inserted

   {post_id: 2, cmt_id:3, usr: 'rob', text: 'Cool!'}
   {post_id: 2, cmt_id:4, usr: 'job', text: 'Cool!'}
}

Is this possible or I am still thinking it in SQL?

If there is alternate approach with same or better performance?

Thanks 开发者_如何学Gofor reading.


First of all you thinking in sql way.. In mongodb to achieve better performance need to embed everything. In your case you should embed comments into blog post like this:

Post
{
    _id,
     PostText,
     Comments { cmt_id, usr, text },
     ...
}

In this case you don't need care about insertion position and possible problems ( comments from one post in different shards) with sharding.

Hope this help!


You're never guaranteed that the comments are stored in order on disk anyway. If post 1 gets a comment, then post 2, then post 1 again the comments will be interleaved on disk, the time between the comments does not matter. As long as you have an index on post_id on the comments collection, loading all comments for a post will always be fast.

The only way to guarantee that the comments are contiguous is to add them as sub documents, but that is unwise. First of all it would mean that the post document would have to be moved on disk often (a very expensive operation in Mongo as the global write lock is held for the whole duration of the move), and secondly something like comments is unbounded, for a very popular post you could very well hit the document size limit, and there would be no way to add more comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消