Home Ask Login Register

Developers Planet

Your answer is one click away!

Sandro Palmieri February 2016

How to related/associate to Mongo Collections in Meteor without using simple schema

I am working on a meteor project.

Step 1

I've added the accounts-password and the accounts-ui packages so in order to have a users collection and an authenticating system.

Step 2

I've created a Mongo collection 'Posts' of documents with the following fields: _id, title, description and createdOn(date).

Step 3

I've created another Mongo collection 'Comments' of documents with the following fields: _id, comment ,postedOn('date') and createdBy(Meteor.user()._id)

Step 4

I've added the iron router package and set some routing. You can view a blog list and go to single post detail page. I want to give the possibility to the users who are logged in to post comments on a single comment without using the aldeed simple-schema package.

Find below some snippets from my project:

        return Posts.find({}, {sort: {createdOn: -1} });
        return Comments.find({ ?????  Ho can I associate comments to a single post? });


I am wondering how can I make the proper association between the 2 collections. I would like to show only those comments associated to the related post. As of now all the comments appear to every post without distinction. Any help? Thanks


Stephen Woods February 2016

You want to add a postId to your comments schema. Then, whenever you're submitting a comment, get the _id of the post in question and send it to your meteor method where you're inserting the comment. Something like this:

// In your template events:
'submitCommentForm': function( event, template ) {
    var postId = this._id; // Make sure your post data context of this form is set in a #each or #with.
    Meteor.call('addComment', event.target.comment, postId, ...) // Assuming your comment is in some sort of named input with comment as the name.

Post Status

Asked in February 2016
Viewed 3,688 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life