dimanche 28 juin 2015

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.

Aucun commentaire:

Enregistrer un commentaire