如何通过Meteor和MongoDB检索相关帖子?

时间:2014-01-23 07:22:27

标签: mongodb meteor

用户的个人资料中包含标记列表。我需要提取与用户标签匹配的所有帖子,然后是剩余的帖子,即使它们与用户的标签不匹配。如果标签匹配最多的帖子也会非常好。

如何通过Meteor实现这一目标?没有聚合框架并且不能一次发布多个游标就像是让我失望。

目前,我的发布功能从用户会话中获取限制参数,并在用户位于列表末尾时增加。

// I would like to return these results

res = [{_id:B},{_id:A},{_ID:C},{_id:D},{_id:E}]

userTags = ['foo', 'qux', 'bar']


Posts

{
  _id: A,
  title: 'Orange',
  tags: ['foo', 'baz']
},
{
  _id: B,
  title: 'Blue',
  tags: ['foo', 'bar', 'baz', 'qux']
},
{
  _id: C,
  title: 'Yellow',
  tags: ['foo']
},
{
  _id: D,
  title: 'Green',
  tags: ['ford']
},
{
  _id: E,
  title: 'Black',
  tags: ['chevy', 'toyota']
}

1 个答案:

答案 0 :(得分:1)

您可以在每个集合中多次调用Meteor.publish。返回的游标然后在客户端上合并。

// server code
Meteor.publish("postsByTag", function () {
  return Posts.find(... match tags ...);
};
Meteor.publish("topTenPosts", function () {
  return Posts.find(... top ten ...);
};

// client code
Meteor.subscribe("postsByTag");
Meteor.subscribe("topTenPosts");
相关问题