如何使用express和mongoose更新嵌套数组值,其中是一个多维数组,需要更新单个对象键值。这是我的数据模型
{
"_id" : ObjectId("5847b92a20352925039432de"),
"user" : {
"email" : "email@mail.io",
"fullName" : "Name"
}
"availabilities" : [
[
{
"status" : "Close",
"end" : "03:00",
"start" : "00:00",
"day" : "Sunday",
"id" : "Sunday00:00"
},
{
"status" : "Close",
"end" : "06:00",
"start" : "03:00",
"day" : "Sunday",
"id" : "Sunday03:00"
},
{
"status" : "Open",
"end" : "09:00",
"start" : "06:00",
"day" : "Sunday",
"id" : "Sunday06:00"
},
{
"status" : "Open",
"end" : "12:00",
"start" : "09:00",
"day" : "Sunday",
"id" : "Sunday09:00"
},
{
"status" : "Open",
"end" : "15:00",
"start" : "12:00",
"day" : "Sunday",
"id" : "Sunday12:00"
},
{
"status" : "Open",
"end" : "18:00",
"start" : "15:00",
"day" : "Sunday",
"id" : "Sunday15:00"
},
{
"status" : "Open",
"end" : "21:00",
"start" : "18:00",
"day" : "Sunday",
"id" : "Sunday18:00"
},
{
"status" : "Open",
"end" : "24:00",
"start" : "21:00",
"day" : "Sunday",
"id" : "Sunday21:00"
}
],
[
{
"status" : "Close",
"end" : "03:00",
"start" : "00:00",
"day" : "Monday",
"id" : "Monday00:00"
},
{
"status" : "Close",
"end" : "06:00",
"start" : "03:00",
"day" : "Monday",
"id" : "Monday03:00"
},
{
"status" : "Open",
"end" : "09:00",
"start" : "06:00",
"day" : "Monday",
"id" : "Monday06:00"
},
{
"status" : "Open",
"end" : "12:00",
"start" : "09:00",
"day" : "Monday",
"id" : "Monday09:00"
},
{
"status" : "Open",
"end" : "15:00",
"start" : "12:00",
"day" : "Monday",
"id" : "Monday12:00"
},
{
"status" : "Open",
"end" : "18:00",
"start" : "15:00",
"day" : "Monday",
"id" : "Monday15:00"
},
{
"status" : "Open",
"end" : "21:00",
"start" : "18:00",
"day" : "Monday",
"id" : "Monday18:00"
},
{
"status" : "Open",
"end" : "24:00",
"start" : "21:00",
"day" : "Monday",
"id" : "Monday21:00"
}
]
]
}
这里我想从可用性数组更改第一个索引,然后是第一个索引对象状态键值。
这是:{
"status" : "Close",
"end" : "03:00",
"start" : "00:00",
"day" : "Sunday",
"id" : "Sunday00:00"
}
答案 0 :(得分:1)
您可以使用positional $ operator或索引号来定位数组条目。
db.students.update(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)
答案 1 :(得分:-1)
yourMoongosemodel.findByIdAndUpdate(id : giveId,
{ $push: {
availabilities[0].status : 'open'
}
}, {new : true}, function(err, data){
console.log(data);
} )