我正在查询所有comments
和commentLikes
. 本质上,我在我的视图中循环浏览评论集合,但我需要commentLikes
与comments
. 现在commentLikes
有一个引用comments._id
as 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 背景,所以我已经习惯了没有关联。