在mongodb中想要三个内部值数组

时间:2017-12-28 05:56:09

标签: javascript node.js mongodb

I have two table in Mongodb with nodejs

                                Daily weight
sub_exercise_id        workout_id              weight       height
111111111111          44444444444              120            74  
111111111111          44444444444              122            74  
222222222222          44444444444              110            75
111111111111          55555555555              121            75
222222222222          55555555555              122            75
222222222222          55555555555              124            76



                                Sub exercise
sub_exercise_id              name
111111111111                Chest
222222222222                Dumbbel


I want this type of response
[   {
        _id:"44444444444"
        exerside_detail{
                {
                    sub_exercise_id:"111111111111",
                    name: "Chest",
                    daily_detail: [
                        {
                            weight:120,
                            height:74
                        },
                        {
                            weight:122,
                            height:74
                        }
                    ]
                },
                {
                    sub_exercise_id:"222222222222",
                    name: "Chest",
                    daily_detail: [
                        {
                            weight:110,
                            height:75
                        }
                    ]
                }
        }

    },
    {
        _id:"55555555555"
        exerside_detail:[
                {
                    sub_exercise_id:"111111111111",
                    name: "Chest",
                    daily_detail: [
                        {
                            weight:121,
                            height:75
                        }
                    ]
                },
                {
                    sub_exercise_id:"222222222222",
                    name: "Chest",
                    daily_detail: [
                        {
                            weight:122,
                            height:75
                        },
                        {
                            weight:124,
                            height:76
                        }
                    ]
                }
        ]

    }
]

1 个答案:

答案 0 :(得分:0)

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创建另一个组