无法获取Mongoose.js 3级Subdocument数组来填充

时间:2015-06-16 14:03:46

标签: node.js mongodb mongoose

我在连接到mongodb的node.js服务器上使用mongoose.js,我有一个像下面这样的猫鼬模型:

var ItemSchema = new Schema({
    qtde: {
        type: Number,
        default: 0.0,
        required: 'Digite a quantidade'
    },
    detalhe: {
        type: Schema.ObjectId,
        ref: 'Detalhe'
    },
    produto: {
        type: Schema.ObjectId,
        ref: 'Produto'
    }
});


var DetalheSchema = new Schema({
    dia: {
        type: String,
        default: '',
        required: 'Digite o Dia do coffee'
    },
    orcamento: {
        type: Schema.ObjectId,
        ref: 'Orcamento'
    },
    itens: [{type: Schema.ObjectId, ref: 'Item'  }]
});

var OrcamentoSchema = new Schema({
    contato: {
        type: String,
        default: '',
        required: 'Digite o nome do Contato',
        trim: true
    },
    curso: {
        type: String,
        default: '',
        required: 'Digite o Curso',
        trim: true
    },
    dias: {
        type: String,
        default: '',
        required: 'Digite os dias do curso',
        trim: true
    },
    inicio: {
        type: String,
        required: 'Digite a Data de Início'
    },
    detalhes: [{type: Schema.ObjectId, ref: 'Detalhe'}]
});

mongoose.model('Item', ItemSchema);
mongoose.model('Detalhe', DetalheSchema);
mongoose.model('Orcamento', OrcamentoSchema);

我的问题是我无法让orcamento.detalhes [n] .itens数组填充甚至加载,它只是一直显示为空数组。

1 个答案:

答案 0 :(得分:0)

不支持嵌套的子文档填充。

https://github.com/LearnBoost/mongoose/issues/601

唯一的方法是填充第1级子文档,然后遍历所有检索到的子文档并填充其中。这里需要注意的是,内部这是填充在mongoose中的确切方式。

或者你可以使用它 https://www.npmjs.com/package/mongoose-deep-populate