How to use where clause for 2 collections join?


#1

I have collections:

Author:{
   _id:
   name:
}

Post:{
   _id:
   postDate:
   description:
   type: // A, B and C
   authorId: // Relation to Author
} 

Comment:{
   _id:
   commentDate:
   description:
   postId: // relation to Post
}

I want get all comment that commentDate > 2015-05-01 and post type = B???
With fields: commentDate, commentDescription, postDescription, postType, authorName to display.
Please help me (any package).


#2

If your query is in a publish function and you want your publication to be reactive, see:
https://hackpad.com/Meteor-reactive-join-publication-packages-MpSFfsZrMxa


#3

I don’t use in publish, my query is in report generation.


#4

If your query doesn’t belong to a publish function (not tested but you get the idea):

  var posts = Post.find(
    { type: 'B' }, 
    { fields: { _id: 1, description: 1, type: 1, authorId: 1 } }
  );
  var postIds = posts.map(function(p) { return p._id; });
  var authorIds = posts.map(function(p) { return p.authorId; });
  var comments = Comment.find(
    { postId: { $in: postIds }, commentDate: { $gt: yourDateHere } }, 
    { fields: { commentDate: 1, description: 1 } }
  );
  var authors = Author.find({ _id: { $in: authorIds} }, { fields: { name: 1 } });

#5

I think that it is slow.