深层人口不使用嵌入式文档

时间:2015-05-18 18:07:16

标签: node.js mongodb mongoose mongoose-populate

我有三份文件如下:

 var TrackAudioSchema = new Schema({
        track_id: {
          type: Number,
        },
            track_path:{
                type:String,
                required:'Please upload audio track'
            }
      });
    mongoose.model('TrackAudio', TrackAudioSchema);

    var trackSchema = new Schema({
                title: {
                    type: String,
                    trim:true,
                    required:'Please fill track title'
                 },
                 version : {
                    type: String,
                    trim: true,
                    default: ''
                 },
                 trackpath:{
                    type: Schema.ObjectId,
                    ref: 'TrackAudio'
                },
        });

    var AlbumSchema = new Schema({
      language: {
            type: String,
            default: '',
            trim: true
        },
        tracks:[trackSchema],
        user: {
            type: Schema.ObjectId,
            ref: 'User'
        },
)};

AlbumSchema.plugin(deepPopulate);
mongoose.model('Album', AlbumSchema);

但是当我尝试使用以下查询从trackpath填充trackSchema时,它不会填充:

Album.findById(albumId)
         .populate('user','name label').
         deepPopulate('tracks.trackpath').
         exec(function(err, albumdetail) {

         }

请帮忙。

1 个答案:

答案 0 :(得分:0)

你错过了:

mongoose.model('Track', TrackSchema);