我的要求是在集合中查找电子邮件ID,如果存在,则需要为该用户添加新的查询详细信息。如果用户不存在,则具有电子邮件ID的新文档和用户的所有其他详细信息应该在集合中创建。
我能够成功创建用户和相应的详细信息。但是当我尝试通过使用电子邮件ID过滤集合时在'inquiry'数组中添加新项目时,它给出验证错误''验证失败:emailID:Path emailID
是必需的。'
我不确定为什么MongoDb会抛出此错误。我不想更新电子邮件ID。我只是想在'inquiry'数组下添加一个新项目
模型是 -
var userEenquirySchema = new Schema({
emailID: {
type: String,
required: true, unique: true, trim: true, match: /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/
},
enquiry: [{
userFullName: { type: String, required: true, unique: false, trim: true},
isdCode: {
type: Number, required: true,
min: [1, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'],
max: [200, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).']
},
primaryPhonenumber: {
type: Number,
validate: {
validator: function (primaryPhonenumber) {
if (isNaN(primaryPhonenumber) || primaryPhonenumber.toString().trim().length != 10) {
return false;
}
},
message: '{VALUE} is not a valid primary phone number!'
},
required: [true, 'User phone number required']
},
procedureName: { type: String, required: true, trim: true },
commuMedium: { type: String, required: false, unique: false, trim: true, default: 'English' },
caseDescription: { type: String, required: true, unique: false, trim: true },
attachmentFlag: { type: String, required: false, enum: ['Y', 'N'], default: 'N' },
attachmentName: { type: String, required: false},
response: [{
mediNovitaResponse: { type: String, required: false, unique: false, trim: true },
respondedBy: { type: String, required: false, unique: false, trim: true },
contactedMethod: { type: String, required: false, enum: ['Phone', 'Email', 'Both']},
emailResponseSent: { type: String, required: false, enum: ['Y', 'N'] },
nextFollowUpDate: { type: Date, required: false, default: Date.now },
followUpAssignee: { type: String, required: false, unique: false, trim: true },
followUpNote: { type: String, required: false, unique: false, trim: true },
respondedAt: { type: Date, required: false, default: Date.now },
}],
updated_at: { type: Date, required: true, default: Date.now }
}]
});
将新项目添加到查询数组的控制器代码 -
userEnquiryModel.findOneAndUpdate({ "emailID": req.body['emailID'] }, {"$push": {
enquiry : [{
userFullName: req.body['userFullName'],
isdCode: parseInt(req.body["isdCode"]),
primaryPhonenumber: parseInt(req.body["primaryPhonenumber"]),
procedureName: req.body["procedureName"],
commuMedium: req.body["commuMedium"],
caseDescription: req.body["caseDescription"],
attachmentFlag: req.body["attachment"],
attachmentName: req.body["attachmentName"],
response: []
}]
}
}