问题
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.
回答1:
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. )
回答2:
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)
来源:https://stackoverflow.com/questions/7512694/embeded-comment-paging-in-mongodb