1

I'm doing a query for all comments and commentLikes. Essentially I'm looping through the comments collection in my view, but I need to associate commentLikes with comments. Right now commentLikes has a reference to comments._id as commentLikes.comment_id.

I was considering using underscore for this as there look to be some nice utility functions, but which ones would I use? I ended up playing with _.where but am not sure if this is the right tool for the job.

Is this the best way to do this?

var comments = 
[ { submission_id: '513d3702b99f8b1ca5000026',
    comment: 'asdfnasdfoiasdfiosdaf',
    user_id: '1',
    _id: '5157e79c562277e457000011',
    deleted: false,
    created: 'Sun Mar 31 2013 00:37:00 GMT-0700 (PDT)',
    parent_id: null },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment: 'asdfioasdfoiasdjiofasdijofjiasodf',
    user_id: '1',
    _id: '5157e799562277e457000010',
    deleted: false,
    created: 'Sun Mar 31 2013 00:36:57 GMT-0700 (PDT)',
    parent_id: null },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment: 'sdafijsadjiofasoijfjaiosdfjioasdfjiosdaf',
    user_id: '1',
    _id: '5157e797562277e45700000f',
    deleted: false,
    created: 'Sun Mar 31 2013 00:36:55 GMT-0700 (PDT)',
    parent_id: null },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment: 'asdfjiadsfojidfsjoaifdaijofdasijods',
    user_id: '1',
    _id: '5157e794562277e45700000e',
    deleted: false,
    created: 'Sun Mar 31 2013 00:36:52 GMT-0700 (PDT)',
    parent_id: null },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment: 'asdfiasdfijoasdfoijas',
    user_id: '1',
    _id: '5157e40227bf651157000280',
    deleted: false,
    created: 'Sun Mar 31 2013 00:21:38 GMT-0700 (PDT)',
    parent_id: null } ];

var commentLikes =
    [ { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e40227bf651157000280',
    user_id: 1,
    _id: '5157e40527bf651157000286',
    created: 'Sun Mar 31 2013 00:21:41 GMT-0700 (PDT)' },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e40227bf651157000280',
    user_id: 1,
    _id: '5157e40b27bf65115700028e',
    created: 'Sun Mar 31 2013 00:21:47 GMT-0700 (PDT)' },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e797562277e45700000f',
    user_id: 1,
    _id: '5157e7cc9c1fe3ed57000001',
    created: 'Sun Mar 31 2013 00:37:48 GMT-0700 (PDT)' },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e799562277e457000010',
    user_id: 1,
    _id: '5157e7cd9c1fe3ed57000002',
    created: 'Sun Mar 31 2013 00:37:49 GMT-0700 (PDT)' },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e79c562277e457000011',
    user_id: 1,
    _id: '5157e7ce9c1fe3ed57000003',
    created: 'Sun Mar 31 2013 00:37:50 GMT-0700 (PDT)' },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e79c562277e457000011',
    user_id: 1,
    _id: '5157e7d19c1fe3ed57000004',
    created: 'Sun Mar 31 2013 00:37:53 GMT-0700 (PDT)' },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e79c562277e457000011',
    user_id: 1,
    _id: '5157e7d29c1fe3ed57000005',
    created: 'Sun Mar 31 2013 00:37:54 GMT-0700 (PDT)' },
  { submission_id: '513d3702b99f8b1ca5000026',
    comment_id: '5157e79c562277e457000011',
    user_id: 1,
    _id: '5157e7d29c1fe3ed57000006',
    created: 'Sun Mar 31 2013 00:37:54 GMT-0700 (PDT)' } ];

var newCommentObj = comments;

_.map(newCommentObj, function(key) {
    // get commentLikes for each comment
    var commentId = key._id,
        likes = _.where(commentLikes, {comment_id : commentId});

});

If not, how should I be doing this? What is the right way to do this with multiple Mongoose collections? I'm coming from a traditional MySQL background, so not having associations is something I'm getting used to.

bob_cobb
  • 2,229
  • 11
  • 49
  • 109

1 Answers1

0

You can do it using underscore's groupBy and map here is the working example.

Also you can combine your collections at "mongodb" using mapReduce look here for more details.

Community
  • 1
  • 1
user20140268
  • 1,313
  • 11
  • 16
  • Hm ok so I think I may have a problem since what is returned isn't actually stringified so `newCommentObj` is returning this: http://pastebin.com/7tUNVCdQ. In this example there are 2 comments and one of them has a comment like. I tried to stringify the `_id` of each comment it but that didn't do anything to help. – bob_cobb Mar 31 '13 at 23:47
  • Actually nevermind. It seems when it prints it to the console it doesn't show the nested likes... Weird. But they are actually being sent to the view. Well that settles that. Thanks. – bob_cobb Apr 01 '13 at 03:02