Embeded comment paging in mongodb

2019-07-21 07:27发布

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.

2条回答
不美不萌又怎样
2楼-- · 2019-07-21 07:41

The biggest question is:

Are your users more interested in comments or in context viewed?

Highly: Put comments in separate documents, and load them first! Then send "secondary" content via AJAX.

Moderately: Use Andrew's solution. (And do not forget that you can also omit fields in queries)

Hardly: Put comments in separate documents, and load them last (via AJAX).

(Also using AJAX can give you nice feature of expanding loaded comments via simple scrolling down)

查看更多
Rolldiameter
3楼-- · 2019-07-21 07:43

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. )

查看更多
登录 后发表回答