我正在对所有comments
和commentLikes
进行查询。基本上我在我的视图中循环浏览评论集合,但我需要将commentLikes
与comments
关联起来。现在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背景,所以没有关联就是我已经习惯的了。