【发布时间】:2016-08-22 05:52:21
【问题描述】:
我有官员模式,如果用户想要修复约会,他的条目会在数据库中进行。架构是:
officerSchema = mongoose.Schema({
email : {type: String,
index: { unique: true }
},
appointmentList : Array // array of jsonObject of dates and userID
});
AppointmentList 是一个 JSON 对象数组,其中包含必须与其进行约会的官员的 ID、日期和用户 ID(想要修复约会的用户)。
但是为了避免重复的约会条目,我一直在使用互联网上提到的几种方法。到目前为止,他们都没有为我工作过。我在下面发布代码。下面代码的问题是它永远不会在约会列表中插入任何数据。但是,如果我使用 save() 而不是 update() 会发生插入,但也会插入重复项。
这是我想从数据库中添加到数组中的 JSON 对象,
{
"id": "1321231231",
"appointment": {
"userID": "31321",
"date": "24 March"
}
}
var ID = requestObject.id;
var newObject = {$addToSet: requestObject.appointment};
OfficerModel.findOne({_id : ID}, function(err, foundData) {
if(err) {
console.log(err);
return;
}
else {
var dbList = foundData.list;
dbList.push(newObject);
foundData.update(function(err, updatedData) {
if(err) {
console.log( err);
}
else {
console.log("successful");
}
});
}
});
【问题讨论】:
标签: json node.js mongodb mongoose mean-stack