将2个MongoDB集合对象与下划线或其他东西组合在一起?

时间:2013-03-31 10:53:07

标签: javascript mongodb mongoose underscore.js

我正在对所有commentscommentLikes进行查询。基本上我在我的视图中循环浏览评论集合,但我需要将commentLikescomments关联起来。现在commentLikes引用comments._id作为commentLikes.comment_id

我正在考虑使用下划线,因为看起来有一些不错的实用功能,但我会使用哪些?我最后玩_.where,但我不确定这是否适合这项工作。

这是最好的方法吗?

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});

});

如果没有,我应该怎么做?什么是使用多个Mongoose集合执行此操作的正确的方法?我来自传统的MySQL背景,所以没有关联就是我已经习惯的了。

1 个答案:

答案 0 :(得分:0)

您可以使用下划线的groupBymap here is工作示例来执行此操作。

您还可以使用mapReduce look here在“mongodb”中合并您的收藏集,以获取更多详情。