Mongo / Mongoose - 如何编辑嵌套结构

时间:2018-05-28 16:02:59

标签: mongodb mongoose

我有一个看起来像这样的集合。每个对象在"天"它是自己的" day"文献。它没有进一步细分。结构如下:

 days: [
        {
            day: 'Sunday', 
            slots: [
                {time: '12:00am', event: null}, {time: '1:00am', event: null}, {time: '2:00am', event: null}, {time: '3:00am', event: null}, 
                {time: '4:00am', event: null}, {time: '5:00am', event: null}, {time: '6:00am', event: null}, {time: '7:00am', event: null}, 
                {time: '8:00am', event: null}, {time: '9:00am', event: null}, {time: '10:00am', event: null}, {time: '11:00am', event: null}, 
                {time: '12:00pm', event: null}, {time: '1:00pm', event: null}, {time: '2:00pm', event: null}, {time: '3:00pm', event: null}, 
                {time: '4:00pm', event: null}, {time: '5:00pm', event: null}, {time: '6:00pm', event: null}, {time: '7:00pm', event: null}, 
                {time: '8:00pm', event: null}, {time: '9:00pm', event: null}, {time: '10:00pm', event: null}, {time: '11:00pm', event: null}, 
            ]
        },
        {
            day: 'Monday', 
            slots: [
                {time: '12:00am', event: null}, {time: '1:00am', event: null}, {time: '2:00am', event: null}, {time: '3:00am', event: null}, 
                {time: '4:00am', event: null}, {time: '5:00am', event: null}, {time: '6:00am', event: null}, {time: '7:00am', event: null}, 
                {time: '8:00am', event: null}, {time: '9:00am', event: null}, {time: '10:00am', event: null}, {time: '11:00am', event: null}, 
                {time: '12:00pm', event: null}, {time: '1:00pm', event: null}, {time: '2:00pm', event: null}, {time: '3:00pm', event: null}, 
                {time: '4:00pm', event: null}, {time: '5:00pm', event: null}, {time: '6:00pm', event: null}, {time: '7:00pm', event: null}, 
                {time: '8:00pm', event: null}, {time: '9:00pm', event: null}, {time: '10:00pm', event: null}, {time: '11:00pm', event: null}, 
            ]
        },
        {
            day: 'Tuesday', 
            slots: [
                {time: '12:00am', event: null}, {time: '1:00am', event: null}, {time: '2:00am', event: null}, {time: '3:00am', event: null}, 
                {time: '4:00am', event: null}, {time: '5:00am', event: null}, {time: '6:00am', event: null}, {time: '7:00am', event: null}, 
                {time: '8:00am', event: null}, {time: '9:00am', event: null}, {time: '10:00am', event: null}, {time: '11:00am', event: null}, 
                {time: '12:00pm', event: null}, {time: '1:00pm', event: null}, {time: '2:00pm', event: null}, {time: '3:00pm', event: null}, 
                {time: '4:00pm', event: null}, {time: '5:00pm', event: null}, {time: '6:00pm', event: null}, {time: '7:00pm', event: null}, 
                {time: '8:00pm', event: null}, {time: '9:00pm', event: null}, {time: '10:00pm', event: null}, {time: '11:00pm', event: null}, 
            ]
        },
        {
            day: 'Wednesday', 
            slots: [
                {time: '12:00am', event: null}, {time: '1:00am', event: null}, {time: '2:00am', event: null}, {time: '3:00am', event: null}, 
                {time: '4:00am', event: null}, {time: '5:00am', event: null}, {time: '6:00am', event: null}, {time: '7:00am', event: null}, 
                {time: '8:00am', event: null}, {time: '9:00am', event: null}, {time: '10:00am', event: null}, {time: '11:00am', event: null}, 
                {time: '12:00pm', event: null}, {time: '1:00pm', event: null}, {time: '2:00pm', event: null}, {time: '3:00pm', event: null}, 
                {time: '4:00pm', event: null}, {time: '5:00pm', event: null}, {time: '6:00pm', event: null}, {time: '7:00pm', event: null}, 
                {time: '8:00pm', event: null}, {time: '9:00pm', event: null}, {time: '10:00pm', event: null}, {time: '11:00pm', event: null}, 
            ]
        },
        {
            day: 'Thursday', 
            slots: [
                {time: '12:00am', event: null}, {time: '1:00am', event: null}, {time: '2:00am', event: null}, {time: '3:00am', event: null}, 
                {time: '4:00am', event: null}, {time: '5:00am', event: null}, {time: '6:00am', event: null}, {time: '7:00am', event: null}, 
                {time: '8:00am', event: null}, {time: '9:00am', event: null}, {time: '10:00am', event: null}, {time: '11:00am', event: null}, 
                {time: '12:00pm', event: null}, {time: '1:00pm', event: null}, {time: '2:00pm', event: null}, {time: '3:00pm', event: null}, 
                {time: '4:00pm', event: null}, {time: '5:00pm', event: null}, {time: '6:00pm', event: null}, {time: '7:00pm', event: null}, 
                {time: '8:00pm', event: null}, {time: '9:00pm', event: null}, {time: '10:00pm', event: null}, {time: '11:00pm', event: null}, 
            ]
        },
        {
            day: 'Friday', 
            slots: [
                {time: '12:00am', event: null}, {time: '1:00am', event: null}, {time: '2:00am', event: null}, {time: '3:00am', event: null}, 
                {time: '4:00am', event: null}, {time: '5:00am', event: null}, {time: '6:00am', event: null}, {time: '7:00am', event: null}, 
                {time: '8:00am', event: null}, {time: '9:00am', event: null}, {time: '10:00am', event: null}, {time: '11:00am', event: null}, 
                {time: '12:00pm', event: null}, {time: '1:00pm', event: null}, {time: '2:00pm', event: null}, {time: '3:00pm', event: null}, 
                {time: '4:00pm', event: null}, {time: '5:00pm', event: null}, {time: '6:00pm', event: null}, {time: '7:00pm', event: null}, 
                {time: '8:00pm', event: null}, {time: '9:00pm', event: null}, {time: '10:00pm', event: null}, {time: '11:00pm', event: null}, 
            ]
        },
        {
            day: 'Saturday', 
            slots: [
                {time: '12:00am', event: null}, {time: '1:00am', event: null}, {time: '2:00am', event: null}, {time: '3:00am', event: null}, 
                {time: '4:00am', event: null}, {time: '5:00am', event: null}, {time: '6:00am', event: null}, {time: '7:00am', event: null}, 
                {time: '8:00am', event: null}, {time: '9:00am', event: null}, {time: '10:00am', event: null}, {time: '11:00am', event: null}, 
                {time: '12:00pm', event: null}, {time: '1:00pm', event: null}, {time: '2:00pm', event: null}, {time: '3:00pm', event: null}, 
                {time: '4:00pm', event: null}, {time: '5:00pm', event: null}, {time: '6:00pm', event: null}, {time: '7:00pm', event: null}, 
                {time: '8:00pm', event: null}, {time: '9:00pm', event: null}, {time: '10:00pm', event: null}, {time: '11:00pm', event: null}, 
            ]
        }
    ]

我想编辑嵌套在这个json中的事件字段。我怎么会这样做?我有一个日模型。但它没有进一步细分。我正在考虑使用findOneAndUpdate,但是我无法弄清楚如何向下钻取所以我在正确的时间段内。端点可以访问将事件设置为的日期,时间和值。

1 个答案:

答案 0 :(得分:0)

使用$运算符在数组

中设置嵌套对象
db.days.update(
    {day: 'Monday', 'slots.time': '12:00am'}
    {$set: {'slots.$.event': 'your new value'}}
);