Javascript 如何在保存另一条记录(Mongoose)时填充和更新记录?

Javascript 如何在保存另一条记录(Mongoose)时填充和更新记录?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,在这个应用程序中,我有3个型号PS、ENG和Patient 创建/更新PS或ENG时,我需要在特定字段中填充和更新患者 发动机型号 'use strict'; const mongoose = require('mongoose'), Schema = mongoose.Schema; const ENGSchema = new Schema({ eng_name: { type: String, required: [true, '

在这个应用程序中,我有3个型号PS、ENG和Patient 创建/更新PS或ENG时,我需要在特定字段中填充和更新患者

发动机型号

'use strict';
const   mongoose = require('mongoose'),
        Schema = mongoose.Schema;

const ENGSchema = new Schema({
    eng_name: {
        type: String,
        required: [true, 'Kindly enter the name of the ENG']
    },
...
    eng_ur: String,//This is Patient number 
    eng_inout: {
        type: String,
        enum: ['Inpatient', 'Outpatient'],
        ref: 'Patient'
    },
    eng_inout_date: { type: Date, ref: 'Patient' },
    eng_inout_loc: { type: String, ref: 'Patient' },
    eng_inout_reason: { type: String, ref: 'Patient' },
...

module.exports = mongoose.model('ENGs', ENGSchema);
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const PatientSchema = new Schema({
...
    patient_status: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
    patient_inout_date: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
    patient_inout_loc: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
    patient_inout_reason: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
...
module.exports = mongoose.model('Patients', PatientSchema);
患者模型

'use strict';
const   mongoose = require('mongoose'),
        Schema = mongoose.Schema;

const ENGSchema = new Schema({
    eng_name: {
        type: String,
        required: [true, 'Kindly enter the name of the ENG']
    },
...
    eng_ur: String,//This is Patient number 
    eng_inout: {
        type: String,
        enum: ['Inpatient', 'Outpatient'],
        ref: 'Patient'
    },
    eng_inout_date: { type: Date, ref: 'Patient' },
    eng_inout_loc: { type: String, ref: 'Patient' },
    eng_inout_reason: { type: String, ref: 'Patient' },
...

module.exports = mongoose.model('ENGs', ENGSchema);
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const PatientSchema = new Schema({
...
    patient_status: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
    patient_inout_date: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
    patient_inout_loc: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
    patient_inout_reason: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'},
...
module.exports = mongoose.model('Patients', PatientSchema);
现在,据我所知,我需要为PS和ENG模型创建一个中间件,比如
pre('save')
,或者我可以在我的控制器中完成

发动机控制器

'use strict';

const   mongoose = require('mongoose'),
        ENG = mongoose.model('ENGs'),
        Patient = mongoose.model('Patients'),...;

exports.create_eng = (req, res)=> {
    let token = getToken(req.headers);
    if(token){
        req.body._user = checkPermissionReturnUserName(token);
        let new_eng = new ENG(req.body);
        new_eng.save((err, eng)=> {
            if (err) res.send(err);
            else{
                res.json(eng);
//Something wrong here?
                        Patient.findOne({patient_ur: this.eng_ur})
                            .populate({select: 'patient_inout_loc', path: new_eng.eng_inout_loc})
                            .exec(function (err, patient) {
                            console.log(patient.patient_inout_loc) //undefined

                        })
//tried this
Patient.findOne({patient_ur: new_eng.eng_ur})
                            .populate({path:'patient_inout_loc', select:'eng_inout_loc', model: ENG})
                            .exec( (err, patient)=> {
                                if (err) res.send(err);
                                else res.json(patient);
                            });
//

            }
        });
    } else return res.status(403).send({success: false, msg: 'Unauthorized.'});

};
这里怎么了

我可以这样做吗(需要能够从PS或ENG填充

patient_inout_loc: {type: Schema.Types.ObjectId, ref: 'ENGs'||'PSs'}

从mongoose文档中找不到它。有什么建议吗?

我想你实际上是指“在引用模型中以及父对象中放置对象”,因为
。populate()
完全是另一回事。该方法用于“检索”而不是“创建”。“预保存”这似乎就是你在这里所说的。那么你实际上是在问如何在
ENG
对象中包含你创建的
Patient
吗?你的实际请求是通过什么方式发生的?例如,一个钩子可以查找添加到
ENG
的“新的”
Patient
,然后“创建”
患者
。但另一种方式会有所不同。我不认为
ref:'ENGs'| |'PSs'
有任何作用。
ref:'ENGs'| |'PSs'
与只说
ref:'ENGs'