当客户端提供新的WEEKNUM时,mongoose.findOneAndUpdate()方法不会创建新对象。相反,它只是更新已经存在于集合中的WEEKNUM和WEEKDATA。我该如何解决这个问题?我在这里缺少什么?
SCHEMA:
{
ageGroup:String,
monthData: [{
month:String,
circleTimeWordsList:[String],
circleTimeTips:[String],
weekData : [{
weekNum : String,
theme : String,
dayData: [{
day:String,
circleTimeSong: String,
circleTimeBook: String,
circleTimeActivity: String,
learningCenter: [{
learningCenterName:String,
learningCenterActivity:String
}]
}]
}]
}]
}
这是我的架构。我使用mongoose.findOneAndUpdate()来插入/更新集合中的数据。
EG。
如果我们在集合中已有以下数据。
{
ageGroup:"2",
monthData: {
month:"January",
circleTimeWordsList:["word 1", "word 2"],
circleTimeTips:["teaching tips", "teaching tips 2"],
weekData : {
weekNum : "1",
theme : "circleTime theme weekly 1",
dayData: [{
day:"Monday",
circleTimeSong: "circleTimeSong 1",
circleTimeBook: "english 1",
circleTimeActivity: "circleTimeActivity 1",
learningCenter: [{
learningCenterName:"Art Center",
learningCenterActivity:"learningCenterActivity 1"
},
{
learningCenterName:"Language Center",
learningCenterActivity:"learningCenterActivity 1"
}]
}]
}
}
}
如果客户端将以下JSON发送到服务器,
{
ageGroup:"2",
monthData: {
month:"January",
circleTimeWordsList:["word 1", "word 2"],
circleTimeTips:["teaching tips", "teaching tips 2"],
weekData : {
weekNum : "2",
theme : "circleTime theme weekly 2",
dayData: [{
day:"Monday",
circleTimeSong: "circleTimeSong 2",
circleTimeBook: "english 2",
circleTimeActivity: "circleTimeActivity 2",
learningCenter: [{
learningCenterName:"Art Center",
learningCenterActivity:"learningCenterActivity 2"
},
{
learningCenterName:"Language Center",
learningCenterActivity:"learningCenterActivity 2"
}]
}]
}
}
}
mongodb集合将如下所示:
{
ageGroup:"2",
monthData: {
month:"January",
circleTimeWordsList:["word 1", "word 2"],
circleTimeTips:["teaching tips", "teaching tips 2"],
weekData : [
{
weekNum : "1",
theme : "circleTime theme weekly 1",
dayData: [{
day:"Monday",
circleTimeSong: "circleTimeSong 1",
circleTimeBook: "english 1",
circleTimeActivity: "circleTimeActivity 1",
learningCenter: [{
learningCenterName:"Art Center",
learningCenterActivity:"learningCenterActivity 1"
},
{
learningCenterName:"Language Center",
learningCenterActivity:"learningCenterActivity 1"
}]
},
{
weekNum : "2",
theme : "circleTime theme weekly 2",
dayData: [{
day:"Monday",
circleTimeSong: "circleTimeSong 2",
circleTimeBook: "english 2",
circleTimeActivity: "circleTimeActivity 2",
learningCenter: [{
learningCenterName:"Art Center",
learningCenterActivity:"learningCenterActivity 2"
},
{
learningCenterName:"Language Center",
learningCenterActivity:"learningCenterActivity 2"
}]
}]
}
]
}
}
}
然而,当前代码只更新旧数据,而不是在WEEKDATA数组中为weekNUM 2创建单独的对象
这是我正在使用的代码:
MyData.getMyDataModelObject().findOneAndUpdate({ageGroup:data.ageGroup, month:data.monthData.month/*, weekNum:data.monthData.weekData.weekNum*/}, data, {upsert:true}, function(err, foundData){});
答案 0 :(得分:0)
假设请求正在进入正文
var data=req.body;
delete data.weekData;
findOneAndUpdate(query,data,function(err,model){
if(err){
// handle here
}
model.monthData.weekData.push(req.body.weekData);
model.save();
});
OR
var data=req.body;
findOneAndUpdate(query,{$push:{"weekData":data.weekData},function(err,model){
if(err){
// handle here
}
});
delete data.weekData;
findOneAndUpdate(query,data,function(err,model){
});
OR
在这里使用mongoose中的子文档你必须在u r架构中进行更改,但如果你执行CRUD操作,这是更好的选择
链接subdocuments