MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

Impossible de remplir le tableau de sous-documents Mongoose.js

essayez ceci

    SubSchema = new Schema({
        _member:     {type: ObjectId, ref: 'Member'},
        members:     [{type: ObjectId, ref: 'Member'}],
        created:     { type: Date, default: Date.now }
    });
    var SubModel = mongoose.model('SubModel', SubSchema);//add

    MainSchema = new Schema({
        _member:     {type: ObjectId, ref: 'Member'},
        subs:        [SubSchema],
        members:     [{type: ObjectId, ref: 'Member'}],
        created:     { type: Date, default: Date.now }
    });

    var MainModel = mongoose.model('MainModel', MainSchema);

    MainModel.find({})
             .sort('created', -1)
             .limit(25)
             .populate("_member")
             .populate("subs._member")
             .exec(function(err, mains){

                 //add
                 SubModel.populate(mains,{
                     path:'subs.members'
                 },function(err,mains){
                    //mains[0].subs[0].members - is not empty
                 });
             });