我有以下数据我想把数据放在下面,但我的架构不允许我做我做的,发送如下数据请检查。当我只放置单个身体时,它的工作正常,但我想要更多的身体
Object.getOwnPropertyNames(obj).reduce((p, c) => {
p[c.replace('|', '')] = obj[c];
return p;
}, {});
//Request body
{
"title" : "test10",
"sch_start": "2017-04-3",
"sch_end":"2017-04-3"
},
{
"title" : "test11",
"sch_start": "2017-04-4",
"sch_end":"2017-04-4"
}
import mongoose, {Schema} from 'mongoose';
/**
* Model to store Calendar entries of Engineer
*/
var EngineerScheduleSchema = new Schema({
title: { type: String, default: ''},
available: { type: Boolean, default: false },
sch_start: { type:Date, default: Date.now },
sch_end: { type:Date, default: Date.now }
});
export default mongoose.model('engineer_schedule', EngineerScheduleSchema);
// Main Model Mongoose Schema
schedule: [EngineerSchedule.schema]
答案 0 :(得分:0)
首先,由于您尝试发送许多计划,因此您的请求正文应如下所示:
[{
"title" : "test10",
"sch_start": "2017-04-3",
"sch_end":"2017-04-3"
},
{
"title" : "test11",
"sch_start": "2017-04-4",
"sch_end":"2017-04-4"
}]
其次,您应该查看 findOneAndUpdate 文档,因为在我看来您发送了两个计划对象( test10 和 test11 )根据 req.params.id 指定的单一计划进行更新。这没有多大意义。
如果您希望在单个请求中更新多个计划,可能应该实施批量更新。看看bulk functionality,我会实现这样的事情:
export function updateSchedulecalendar(req, res) {
var responseSchedule;
var bulk = db.items.initializeUnorderedBulkOp();
// Iterate over every schedule sent by client
for(schedule in req.body) {
// Generate a new update statement for that specific document
bulk.find( { title: schedule.title } ).update( { $set: { sch_start: schedule.sch_start, ... } } );
}
// Execute all updates
bulk.execute().then(function() {
// Validation
});
}