Json 无法将数据插入到集合中
我有一个officer模式,在这个模式中,如果用户想要确定一个约会,他的条目将在DB中进行。模式是:Json 无法将数据插入到集合中,json,node.js,mongodb,mongoose,mean-stack,Json,Node.js,Mongodb,Mongoose,Mean Stack,我有一个officer模式,在这个模式中,如果用户想要确定一个约会,他的条目将在DB中进行。模式是: officerSchema = mongoose.Schema({ email : {type: String, index: { unique: true } }, appointmentList : Array // array of jsonObject of dates and userID }); AppointmentList是一个JSON对
officerSchema = mongoose.Schema({
email : {type: String,
index: { unique: true }
},
appointmentList : Array // array of jsonObject of dates and userID
});
AppointmentList是一个JSON对象数组,其中包含必须进行约会的官员的ID、日期和用户ID(希望修复约会的用户)
然而,为了避免重复的预约条目,我一直在使用互联网上提到的几种方法。到目前为止,他们都没有为我工作过。我把代码贴在下面。下面代码的问题是它从不在AppointsList中插入任何数据。但是,如果使用save()而不是update()插入,则会发生插入,但也会插入重复项
下面是我想从DB添加到数组中的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");
}
});
}
});
使用操作符可能对您有用
var appt = {
id: "1321231231",
appointment: {
userID: "31321",
date: "24 March"
}
}
Officer.update(
{_id: ID},
{$addToSet: {appointmentList: appt}},
function(err) { ... }
);
但这不是一个完美的解决方案,因为{one:1,two:2}和{two:2,one:1}没有被解释为相等,所以它们都可以被添加到一个带有$addToSet的数组中
要完全避免重复,可以执行以下操作:
var appt = {
id: "1321231231",
appointment: {
userID: "31321",
date: "24 March"
}
};
Officer.findOne(
{_id: ID, 'appointmentList.id': appt.id},
function(err, officerDoc) {
if (err) { ... }
// since no document matched your query, add the appointment
if (!officerDoc) {
Officer.update(
{_id: ID},
{$push: {appointmentList: appt}},
function(err) { ... }
);
}
// since that appointment already exists, update it
else {
Officer.update(
{_id: ID, 'appointmentList.id': appt.id},
{$set: {'appointmentList.$.appointment': appt.appointment}},
function(err) { ... }
);
}
}
);
上面更新现有约会的操作使用。使用操作员可能适合您
var appt = {
id: "1321231231",
appointment: {
userID: "31321",
date: "24 March"
}
}
Officer.update(
{_id: ID},
{$addToSet: {appointmentList: appt}},
function(err) { ... }
);
但这不是一个完美的解决方案,因为{one:1,two:2}和{two:2,one:1}没有被解释为相等,所以它们都可以被添加到一个带有$addToSet的数组中
要完全避免重复,可以执行以下操作:
var appt = {
id: "1321231231",
appointment: {
userID: "31321",
date: "24 March"
}
};
Officer.findOne(
{_id: ID, 'appointmentList.id': appt.id},
function(err, officerDoc) {
if (err) { ... }
// since no document matched your query, add the appointment
if (!officerDoc) {
Officer.update(
{_id: ID},
{$push: {appointmentList: appt}},
function(err) { ... }
);
}
// since that appointment already exists, update it
else {
Officer.update(
{_id: ID, 'appointmentList.id': appt.id},
{$set: {'appointmentList.$.appointment': appt.appointment}},
function(err) { ... }
);
}
}
);
上面更新现有约会的操作使用了。几个问题:“appointmentList.id”可以告诉mongoose查看名为appointmentList的表/集合字段,然后遍历AppointList字段拥有的所有JSONObject,并将它们与提供的id匹配,对吗?如果是这样,这意味着如果我们有like appointmentList=[{a:1,[{b:7}]},…]我们可以通过appointmentList.a.b检查“b”?如果没有,我们如何访问b?有没有关于这方面的教程?有几个问题:“AppointList.id”可以告诉mongoose,它查看名为appointmentList的表/集合字段,然后遍历AppointList字段拥有的所有JSONObject,并将它们与提供的id匹配,对吗?如果是这样,这意味着如果我们有like appointmentList=[{a:1,[{b:7}]},…]我们可以通过appointmentList.a.b检查“b”?如果没有,我们如何访问b?有关于这方面的教程吗?