SkinnyGeek1010
SkinnyGeek1010

Reputation: 536

How can I retrieve relevant posts with Meteor and MongoDB?

A user has a profile with a list of tags. I need to pull in all posts that match the user's tags, followed by the remaining posts, even if they don't match the user's tags. It would be really nice to also have the posts with the most tag matches toward the front.

How can I accomplish this with Meteor? Not having the aggregation framework and not being able to publish multiple cursors at once is kind of tripping me up.

Currently my publish function takes a limit param from the user's session and is increased when the user is toward the end of the list.

// 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']
}

Upvotes: 0

Views: 57

Answers (1)

avital
avital

Reputation: 1542

You can call Meteor.publish more than once on each collection. The cursors returned then get merged on the client.

// 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");

Upvotes: 1

Related Questions