Javascript 如何处理;关系“;用MongoDB/流星

Javascript 如何处理;关系“;用MongoDB/流星,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我仍然在我的Meteor.js应用程序上,我们想在我的页面上显示当前用户朋友的所有帖子 目前,我只是简单地将每一篇文章展示如下: talkthreads: function(){ return Posts.find({parent: null},{sort: {date: -1}}); } 但是我想做一些尽可能简单/高效的事情来过滤它们,并且只从用户的朋友那里得到一个 诸如此类: talkthreads: function(){ return Posts.find({par

我仍然在我的Meteor.js应用程序上,我们想在我的页面上显示当前用户朋友的所有帖子

目前,我只是简单地将每一篇文章展示如下:

talkthreads: function(){
  return Posts.find({parent: null},{sort: {date: -1}});
}
但是我想做一些尽可能简单/高效的事情来过滤它们,并且只从用户的朋友那里得到一个

诸如此类:

talkthreads: function(){
        return Posts.find({parent: null, owner: [match one of my friend id]}, {sort: {date: -1}});
    }
就像我使用SQL一样

另一点是,我目前将我的帖子集合发布到所有客户端。但由于它的目标是随着时间的推移而增长,我不想把所有的帖子都发布给每个客户

我怎么能只发布和订阅我或我朋友的帖子,而且数量有限:我不想一次加载超过最后15篇帖子。当我点击一个a按钮时,我会再加载15条(比如在FB上,当你在页面底部滚动时,它会自动附加旧帖子)


谢谢您的帮助。

我假设您的用户有一组好友id,您可以使用这些id进行查询。在本例中,您希望执行如下查询:

 Posts.find({parent: null, owner: Users.find({_id: user_is}, {friends: 1, _id: 0})}, {sort: {date: -1}});

基本上,使用Mongo,您可以嵌套您的搜索,就像这样,从而获得所需的数据

您要求的是客户端连接。假设
Meteor.user().profile.friends
是一个用户ID数组,类似这样的内容应该在您的助手中起作用:

talkthreads: function() {
  // select owners who are friends of the current user
  // see the publish example if you want to include your own posts
  var owners = Meteor.user().profile.friends || [];

  var selector = {
    parent: null,
    owner: {$in: owners}
  };

  return Posts.find(selector, {sort: {date: -1}});
}
问题的后半部分是关于分页的。这可能最好作为一个单独的问题来问,但这里有一个关于如何设置出版商的想法:

var POSTS_PER_PAGE = 15;

Meteor.publish('paginatedPosts', function(pageNumber) {
  // fetch the current user because Meteor.user() isn't available here
  var user = Meteor.findOne(this.userId);

  // get an array of user ids for the user's friends
  var owners = user.profile.friends || [];

  // also add the current userId to the list of owners
  owners.push(this.userId);

  var selector = {
    parent: null,
    owner: {$in: owners}
  };

  // publish a limited set of posts based on the current page
  var options = {
    limit: POSTS_PER_PAGE * pageNumber,
    sort: {date: -1}
  };

  return Posts.find(selector, options);
});
在客户端上,您需要跟踪当前页面(从1开始,每次他/她单击“加载更多”按钮时递增),并在页码更改时激活订阅。例如:

Tracker.autorun(function() {
  var pageNumber = Session.get('currentPage') || 1;
  Meteor.subscribe('paginatedPosts', pageNumber);
});

当然,这可能是模板自动运行,也可能是全局运行,也可能是在路由器中运行,这取决于对应用程序有何意义。

顺便说一句,我在手机上键入了此内容,但没有检查以确保其格式正确且工作正常。但是它应该让你了解嵌套搜索的概念,所以我不得不在owner元素中添加朋友id的数组?此数组位于user().profile.friends中当前您刚刚创建了对象的所有者搜索,仅使用Users.findOne(user_id).friends不是更好吗?非常感谢您提供的提示。考虑到这两点在我脑海中提出了其他问题,我将分别问他们。再次感谢