gpt4 book ai didi

javascript - 用下划线或其他东西组合 2 个 MongoDB 集合对象?

转载 作者:行者123 更新时间:2023-11-28 09:15:24 25 4
gpt4 key购买 nike

我正在查询所有评论commentLikes。本质上,我在 View 中循环浏览评论集合,但我需要将 commentLikescomments 关联起来。现在,commentLikescomments._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 背景,所以我已经习惯了没有关联。

最佳答案

您可以使用下划线的groupBymap here is来做到这一点工作示例。

您还可以使用mapReduce在“mongodb”合并您的集合 look here了解更多详情。

关于javascript - 用下划线或其他东西组合 2 个 MongoDB 集合对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com