简体   繁体   中英

Mongoose update not working with embedded objects

I'm working in the MEAN stack, and I'm stuck on a Mongoose issue. Updates are working fine for individual documents(single key:value), but when I attempt to update a nested object with a new object, it's just deleting the original object and not inserting the new one.

Using an identical query directly in mongo, within the terminal, works perfectly. But from my Mongoose model, I get the above behavior.

My final desired query is more complicated and uses many variables, so I've simplified the code to the following to highlight the issue:

Setup.update({name: "main"}, {$set: {"schedule.sunday.eleven_pm": { associates: 111, supervisors: 111}}}, function(err){
            if(err){
                console.log(err);
            }
            else{
                console.log('successfully updated main schedule setup')
                Setup.find({}, function(err, setup){
                    if(err){
                        console.log(err);
                    }
                    else{
                        res.json(setup);
                    }
                })
            }
        });

Gives me this in my db:

"eleven_pm" : {

        }

But from terminal, the same query (cut and pasted from my mongoose query, just added 'db.setups' to beginning:

db.setups.update({name: "main"}, {$set: {"schedule.sunday.eleven_pm": { associates: 111, supervisors: 111}}})

Gives me this, the desired result:

    "eleven_pm" : {
            "associates" : 111,
            "supervisors" : 111
        }

I've tried writing this as a findOneAndUpdate(), but encountered the same behavior. Am I doing something unorthodox here?

Any help greatly appreciated.

我的Mongoose“ Schema”中未对数据库结构进行小的更改,并且两者之间的差异导致了上面解释的异常查询行为。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM