【问题标题】:Population to sub-scheme in Mongoose猫鼬中的人口到子计划
【发布时间】:2013-11-09 18:50:11
【问题描述】:

我有两个架构:

诊所:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ProcedureSchema = mongoose.Schema({
    name: {
        type: String,
        trim: true,
        required: true
    },

    alias: {
        type: String,
        trim: true,
        required: true
    }
});

var ClinicSchema = mongoose.Schema({
    name: {
        type: String,
        trim: true,
        required: true
    },

    procedures: [ProcedureSchema]
});

module.exports = mongoose.model('Clinic', ClinicSchema);

并记录:

var mongoose = require('mongoose'),
    Patient = require('./patient'),
    User = require('./user'),
    Clinic = require('./clinic'),
    Schema = mongoose.Schema;

var RecordSchema = Schema({
    doctor: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },

    clinic: {
        type: Schema.Types.ObjectId
    },

    date: {
        type: Date
    },

    patient: {
        type: Schema.Types.ObjectId,
        ref: 'Patient'
    },

    procedure: {
        type: [Schema.Types.ObjectId],
        ref: 'Clinic'
    }
});

module.exports = mongoose.model('Record', RecordSchema);

在记录模式中,我存储了程序的所有 ID,它是诊所的子模式 我想记录完整的程序对象。 我试试这个查询:

Record.find({}).
    populate('procedures.procedure').
    populate('doctor').
    populate('patient').
    exec(function(err, records) {
        ...
    });

但只获取 id 数组,而不是对象数组。 问题出在哪里?

【问题讨论】:

    标签: mongodb mongoose populate


    【解决方案1】:

    你完全混合了所有方案:

    填充('procedures.procedure')

    但是你在 RecordSchema 中没有程序。即使是类型错误,您的意思是 procedure.procedures - 您在 ProcedureSchema 中没有过程。

    阅读更多关于 MongoDB 中的参考资料,尤其是 http://docs.mongodb.org/manual/applications/data-models-tree-structures/

    尝试使嵌套路径小于 2。像这样:

    var User,
    Procedure,
    Clinic,
    Patient,
    Record;
    
    function defineModels(mongoose, fn) {
    var Schema = mongoose.Schema,
        ObjectId = Schema.ObjectId;
    
    User = new Schema({
        ...
    });
    
    Procedure = new Schema({
        name: { type: String, trim: true, required: true },
        alias: { type: String, trim: true, required: true }
    });
    
    Clinic = new Schema({
        name: { type: String, trim: true, required: true },
        procedures: [ProcedureSchema]
    });
    
    Patient = new Schema({
        ...
    });
    
    Record = new Schema({
        'date': {type: Date, default: Date.now},
        'doctor': {type: ObjectId, ref: 'User'},
        'clinic': {type: ObjectId, ref: 'Clinic'},
        'patient': {type: ObjectId, ref: 'Patient'},
        'procedure': {type: ObjectId, ref: 'Procedure'},
    });
    
    mongoose.model('User', User);
    mongoose.model('Procedure', Procedure);
    mongoose.model('Clinic', Clinic);
    mongoose.model('Patient', Patient);
    mongoose.model('Record', Record);
    fn();
    }
    
    exports.defineModels = defineModels;
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 2017-11-19
      • 2015-05-14
      • 1970-01-01
      • 2021-04-12
      • 2021-04-11
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多