3

我的文档有一个文档,它有一个文档。我也想知道如何填充内部文档。我已经尝试过了,但它没有填充我的内部文档。

        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},
});
4

2 回答 2

0

Mongoose 不支持嵌套种群。因此这部分是无效的:.populate('owner.company')

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

于 2015-04-17T17:25:52.970 回答
0

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

于 2012-05-31T22:23:04.737 回答