【问题标题】:Wanted three inside values array in mongodb在 mongodb 中需要三个内部值数组
【发布时间】:2017-12-28 05:56:09
【问题描述】:
我在 Mongodb 中有两个带有 nodejs 的表 每日体重 sub_exercise_id 锻炼_id 体重 身高 111111111111 44​​444444444 120 74 111111111111 44​​444444444 122 74 222222222222 44444444444 110 75 111111111111 55555555555 121 75 222222222222 55555555555 122 75 222222222222 55555555555 124 76 子练习 sub_exercise_id 名称 111111111111 胸部 222222222222 哑铃 我想要这种类型的回应 [ { _id:“44444444444” exerside_detail{ { sub_exercise_id:"111111111111", 名称:“胸部”, 每日详细信息:[ { 重量:120, 身高:74 }, { 重量:122, 身高:74 } ] }, { sub_exercise_id:"222222222222", 名称:“胸部”, 每日详细信息:[ { 重量:110, 身高:75 } ] } } }, { _id:"55555555555" exerside_detail:[ { sub_exercise_id:"111111111111", 名称:“胸部”, 每日详细信息:[ { 重量:121, 身高:75 } ] }, { sub_exercise_id:"222222222222", 名称:“胸部”, 每日详细信息:[ { 重量:122, 身高:75 }, { 重量:124, 身高:76 } ] } ] } ]

【问题讨论】:

标签: javascript node.js mongodb


【解决方案1】:
var group = {
    $group: {
        _id: {
            main_id: '$workout_id',
            sub_exercise_id: '$sub_exercises._id'
        },
        exercise_detail: {
            $push: {
                id: '$_id',
                weight: '$weight',
                reps: '$reps',
                sets: '$sets',
                auto_finish: '$auto_finish',
                created_at: '$created_at',
                time: '$time'
            }
        }
    }
};

var group1 = {
    $group: {
        _id: '$_id.main_id',
        exercise_detail: {
            $push: {
                sub_exercise_name: '$_id.sub_exercise_id',
                exercise_detail1: '$exercise_detail'
            },
        }
    }
};
  1. 您需要创建具有唯一条件值的组。
  2. 使用 $addtoset 创建另一个组

【讨论】:

    猜你喜欢
    • 2020-11-13
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多