【问题标题】:Node.js Mongoosejs populate a populated field [duplicate]Node.js Mongoosejs 填充填充字段 [重复]
【发布时间】:2015-04-18 01:25:52
【问题描述】:

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

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

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-10-08
        • 2015-01-20
        • 2012-01-22
        • 2017-02-13
        • 2021-05-06
        • 1970-01-01
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        相关资源
        最近更新 更多