gpt4 book ai didi

arrays - 无法将值推送到 mongoDB 数组

转载 作者:可可西里 更新时间:2023-11-01 09:49:37 25 4
gpt4 key购买 nike

嗨,我有一个 MEAN 堆栈应用程序,它使用如下定义的 Mongoose 模型:

约会.js

var mongoose = require('mongoose');
module.exports = mongoose.model('Appointment', {
appt_date: String,
details: [ {
appt_time: String,
patient_id : String,
patient_name : String,
contact_no: String,
doctor_name : String,
purpose : String
}
]
});

我想生成如下文档:

{
appt_date: 13/13/2013,
details: [
{
appt_time: 09:00 AM,
patient_id: 2015/2016,
patient_name: David John,
contact_no: 8965741234,
doctor_name: Albert ,
purpose: Consultation,

}
}

使用以下 Express JS 代码时无法生成文档:

var Appointment = require('../../models/appointment');
var appointments = new Appointment ({ "appt_date" : req.body.date});
appointments.save();
Appointment.update({ appt_date: req.body.date},
{
$push: {
'details': {
appt_time:"09:00 AM",
patient_id:"2015/2016",
patient_name:"Wilfred",
contact_no:"8965741234",
doctor_name: "Albert",
purpose: "Consultation"
}
}
},
{
upsert:true
});

经过以上操作,得到的结果如下:

"_id": ObjectId('57b6eefd2b494e802ba146d8'),
"appt_date": "12/20/2014",
"details": [],
"__v": 0

我无法将任何数据推送到“详细信息”数组。我需要的是一个日期条目(appt_date),多个详细信息(详细信息数组)。请帮忙解决这个问题。

谢谢。

最佳答案

save() 后无需更新,只需使用 push() details 键上的方法,然后保存。例如:

var Appointment = require('../../models/appointment');
var appointments = new Appointment ({ "appt_date" : req.body.date});
appointments.details.push({
appt_time: "09:00 AM",
patient_id: "2015/2016",
patient_name: "Wilfred",
contact_no: "8965741234",
doctor_name: "Albert",
purpose: "Consultation"
});
appointments.save(function(err, appointment){
if (err) handleErr(err);
console.log(appointment);
});

关于arrays - 无法将值推送到 mongoDB 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com