Embeded comment paging in mongodb

你说的曾经没有我的故事 提交于 2019-12-24 04:32:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!