mongodbpaginationcomments

Embeded comment paging in mongodb


if I got a collection for storing Articles with it's Comments embedded, when retriving data from db, I will get a Article object with a completely Comment list, support there are a lot of comments, so this could be a problem of loading efficience, how can I handler this by paging Comments? do I have to use a seperate collection for Comments? or what else? thanx in advance.


Solution

  • You looking for the $slice operator.

    To retrieve comments by paging you need code like this:

    db.articles.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10 
    

    This operation will return articles with only sliced comments. )