0

I'm adapting the Microscope tutorial to my needs and am having difficulty writing a query which gathers all the comments embedded within a post.

Here is my query so far: Posts.find({"postId": this._id}, {"comments":{}});

Here is an example post I want to get the comments from:

{
"_id": "Ad9RYqWqbsJKZx3h7",
  "title": "Writing decimal number words as a decimal number",
  "userId": "9yqTaFeQSqvKmNn8B",
  "author": "Sacha Greif",
  "submitted": "2017-01-05T03:26:18.908Z",
  "commentsCount": 4,
  "comments": [
{
  "body": "Hello",
  "postId": "Ad9RYqWqbsJKZx3h7",
  "userId": "73qGvsRuqNtXcaZDx",
  "author": "student",
  "submitted": "2017-01-05T10:26:45.745Z"
},
{
  "body": "How are you?",
  "postId": "Ad9RYqWqbsJKZx3h7",
  "userId": "73qGvsRuqNtXcaZDx",
  "author": "student",
  "submitted": "2017-01-05T10:28:17.225Z"
}
  ]}

It seems to return a cursor, but I am not able to use the toArray() function on it.

I have read this is not possible (Filtering embedded documents in MongoDB), but this was six years ago...

I've also seen posts about $slice and aggregate but can't seem to get me head around them.

Any help much appreciated!

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
sutebu
  • 13
  • 3
  • Are you subscribing to the data? Why not simply use `PostsfindOne(postId)` and extract all of the comments from the received object? – MasterAM Jan 05 '17 at 13:54
  • The data is published: `Meteor.publish('comments', function(postId) { check(postId, String); return Posts.find({postId: postId}); });` and here is the subscription in router.js `Router.route('/posts/:_id', { name: 'postPage', waitOn: function() { return [ Meteor.subscribe('singlePost', this.params._id), Meteor.subscribe('comments', this.params._id) ]; }, data: function() { return Posts.findOne(this.params._id); } });` – sutebu Jan 05 '17 at 22:36
  • How would I go about extracting the comments from the received object? Something like: `var post = Posts.findOne({postId: this._id}); var commentList = post.comments.toArray(); return commentsList;`? Thanks MasterAM. – sutebu Jan 05 '17 at 22:39
  • In the publication, you need to query by _id, not postId. – MasterAM Jan 05 '17 at 22:58
  • I added autopublish in case this was the issue. Still no luck though... – sutebu Jan 06 '17 at 01:13
  • Have you tried querying `Posts.findOne(this._id);`? If you have and get no results, please **edit your question** to include all of the relevant code for reproducing this. No need to call `toArray()` of whatever, as the `comments` property is already an array. – MasterAM Jan 06 '17 at 11:11

2 Answers2

0

query this-> Posts.find({"comments.postId": this._id});

0

You can just simply use findOne to get the specific document from the collection and you can use forEach Loop to iterate through the comments array of that document.

var post = Posts.findOne({_id: "<id_of_the_post>"});

var post_comments = post.comments; // post_comments contains the comments array

or if you want to do something with each comment use forEach

    post_comments.forEach(function(entry, index, arr){
    // entry has the comment object 
//do something with the comment
    });

I hope this helps.

Piyush Kumar
  • 481
  • 1
  • 6
  • 17