MongoDB查询帮助:更新嵌套数组中的元素

时间:2016-10-17 15:59:01

标签: javascript node.js mongodb

我正在尝试更新'日程安排'。建筑物,房间等可能已经存在,也可能不存在。

这是我正在使用的电话:

var query ={code: dayObj.code, rooms: {$elemMatch: {name: dayObj.room, days: {$elemMatch: {date: dayObj.day}}}}},
        update ={$push: {'rooms.???.days.$' : newDay} },
        options = {upsert: true};
    Building.findOneAndUpdate(query, update, options, callback());

我对我的更新参数应如何引用房间以及当天感到困惑。更新参数的范围是什么?我假设它是找到/创建的整个文档?

型号:

var daySchema = new mongoose.Schema({
   schedule: [{
       time: {type: String, required: true},
       activity: {type: String, required: true}
   }]
});

var roomSchema = new mongoose.Schema({
    name: {type: String, required: true},
    days: [{
        date: {type: String, required: true},
        schedule: {type: daySchema, required: true}
    }]
});

var buildingSchema = new mongoose.Schema({
    //name: {type: String, required: true}
    code: {type: String, required: true},
    rooms: [roomSchema]
});

0 个答案:

没有答案