2013-05-20 22 views
29

我有與由一個參考到另一個收集和數字的嵌套陣列的對象的數組lists一個貓鼬模式對象的陣列填充:貓鼬用含有REF

var Schema, exports, mongoose, schema; 

mongoose = require("mongoose"); 

Schema = mongoose.Schema; 

schema = new Schema({ 
    name: { 
    type: String, 
    required: true, 
    unique: true, 
    trim: true 
    }, 
    lists: [ 
    { 
     list: { 
     type: Schema.ObjectId, 
     require: true, 
     ref: "List" 
     }, 
     allocations: [ 
     { 
      type: Number, 
      required: true 
     } 
     ] 
    } 
    ], 
    createdAt: { 
    type: Date, 
    "default": Date.now 
    }, 
    updatedAt: { 
    type: Date 
    } 
}); 

exports = module.exports = mongoose.model("Portfolio", schema); 

然而,我不能得到populate按預期方式工作,沒有得到TypeError: Cannot read property 'ref' of undefined。我試過populate('list')populate('lists list'),但我要麼不正確地調用東西,要麼我的架構沒有正確地形成。

lists: [ 
    { 
     type: Schema.ObjectId, 
     require: true, 
     ref: "List" 
    } 
    ] 

,但我希望有分配陣列並排名單:如果我只是自己引用的名單,我沒有這個問題。我需要做些什麼來獲得我想要的行爲?

回答

46

我找到了答案:populate('lists.list')的作品。感謝這個問題:Mongoose populate within an object?

+1

嗨,朋友,這是不適合我任何我想念的事情。我沒有使用必需的。 –