Mongoose通过推送多个refs创建

时间:2017-04-06 10:01:29

标签: node.js mongodb mongoose mongoose-schema mongoose-populate

所以我的DB有一种嵌套结构,这是我的三个模式: 用户架构:

[b for _, b in sorted(enumerate(arr, 1), key=lambda x: int.__mul__(*x))]

程序架构:

var UserSchema = new mongoose.Schema({

    email: {
        type: String,
        lowercase: true,
        unique: true,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    role: {
        type: String,
        enum: ['trainer', 'client', 'admin'],
        default: 'client'
    },
    trainer: {
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'User'
    },
    programs:  [
       {
                type: mongoose.Schema.Types.ObjectId, 
                ref: 'Program'
            }
        ],
    diet:   [
    {
        calories: {
            type: Number
        },
        protein: {
            type: Number
        },
        fats: {
            type: Number
        },
        carbs: {
            type: Number
        },
        fiber: {
            type: Number
        }
    }],
    stats: 
       {
          bodyweight: 
              [ {
                  _id: false,
                  measurement:{ type: Number},
                 time : { type : Date, default: Date.now }
              }
              ]
        },
    name: {
        type: String,
        required: true
    },

}, {
    timestamps: true
});

锻炼架构:

var ProgramSchema = new mongoose.Schema({

    title: {type: String, required: true},
    description: {type: String, required: true},
    createdby: {
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'User'
    }, 
    exercises: [
        {
            exercise:{
                type: mongoose.Schema.Types.ObjectId, 
                ref: 'Exercise'
            },
            sets:{
                type: Number
            }
        } 
        ]
}, {
    timestamps: true
});

如您所见,用户可以拥有许多程序,而程序可以有很多练习。但是当我返回一个用户对象时,即使使用populate函数,也不会填充用户程序中的练习。我相信这取决于我如何创建一个程序,从我的理解我必须将练习对象推入程序,但我不确定如何创建一个程序并同时将练习推入其中。这是我目前的创建程序程序函数:

var ExerciseSchema = new mongoose.Schema({

    name: {
        type: String,
        lowercase: true,
        unique: true,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    video: {
        type: String
    },
    image: {
        type: String
    }

}, {
    timestamps: true
});

有没有办法可以在req.body.exercises上做一些forEach? 提前致谢 这也是我如何回报用户:

exports.createProgram = function(req, res, next){

    Program.create({
        title : req.body.title,
        description: req.body.description,
        exercises: req.body.exercises,
        createdby: req.body.createdby

    }, function(err, program) {

        if (err){
            res.send(err);
        }

        Program.find(function(err, programs) {

            if (err){
                res.send(err);
            }

            res.json(programs);

        });

    });

}

1 个答案:

答案 0 :(得分:2)

我认为你走的是正确的道路,但你populated exercise错了。 您需要填充exercises

,而不是填充exercises.exercise

试试这个:

User.find({trainer: trainerid})
    .populate({
        path: 'programs',
        populate: { path: 'exercises.exercise' }
    })
    .exec(function (err, clients) {
        if (err) return handleError(err);
    console.log(clients);
    res.json(clients);
    });