Node.js Mongoosejs填充一个填充的字段

时间:2012-05-25 22:13:42

标签: node.js mongoose

我的文档有一个包含文档的文档。我想知道如何填充内部文档。我试过这个,但它没有填充我的内心文件。

        Document.find({})
        .populate('owner')
        .populate('owner.company')
        .run(function(err,docs){
        if(err){
            req.flash('error', 'An error has occured. Please contact administrators.')
        }
        console.log(docs);
        res.render('dashboard/index', { title: 'Dashboard', menu: 'Dashboard', docs: docs});
    });

var mongoose = require("mongoose"),
    Schema   = mongoose.Schema,
    ObjectId = Schema.ObjectId,
    DocumentObjectId = mongoose.Types.ObjectId;

var Document = new Schema({
    filepath: {type: String, required: true},
    createdBy: {type: String, required: true},
    created: {type: Date, default: Date.now},
    owner: {type: ObjectId, ref: 'owner'}
});

var Owner = new Schema({
    fullname: {type: String, required: true},
    company: {type: ObjectId, ref: 'company'}
});

var Company = new Schema({
    name: {type: String, required: true},
});

2 个答案:

答案 0 :(得分:0)

似乎这是一个在github上打开的#601错误。如果他们到达下一个版本,将不得不等待修复。

答案 1 :(得分:0)

Mongoose不支持嵌套人口。因此,此部分无效:.populate('owner.company')

有一个插件:https://github.com/buunguyen/mongoose-deep-populate