0
用户在我的网站上分享帖子,每篇文章都有一个城市和类别以及投票选项。我在顶部有一个菜单帮助用户过滤城市,类别,顶级投票和最新的帖子。我正试图做的是:连接的MongoDB查询
- 如果用户更改城市,那么我将返回该城市的所有帖子。
- 如果此用户也更改类别。以及我必须返回该类别的帖子,但也是该城市。
- 如果用户选择了最新版本,我必须返回该城市中该类别的最新帖子。
- 而我在城市名单中有一个特殊选项。哪些应该在所有城市中返回帖子。并在类别列表中。
我使用流星&落得这样做面条:
if (Session.get('topPosts')){
if(Session.get("selectedCity") && Session.get("selectedCategory")){
if (Session.get("selectedCity") === Cities[0] && Session.get("selectedCategory") === Categories[0]){
return Posts.find({}, {sort: {score: -1}});
}
else if (Session.get("selectedCity") === Cities[0]){
return Posts.find({ category: Session.get("selectedCategory") }, {sort: {score: -1}});
}
else if (Session.get("selectedCategory") === Categories[0]){
return Posts.find({ city: Session.get("selectedCity") }, {sort: {score: -1}});
}
return Posts.find({ city: Session.get("selectedCity"), category: Session.get("selectedCategory") }, {sort: {score: -1}});
}
..
我是新来的MongoDB。我不知道是否可以通过简单查询或多个查询之间的组合来实现此目的。