Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose not updating embedded document

I quite a problem while I'm attempting to update an embedded document in mongodb. I've tried two methods and neither work, and I've searched everywhere for reasons why this it's not updating. Anyway, my schema looks like this (I may note that the embedded document I'm trying to update is a Mixed type).

var UserModel = new mongoose.Schema({
    account: String,
    salt: String,
    password: String,
    highlight_words: String,
    networks: {},
    ip: String,
    ident: String,
    is_connected: Boolean,
    account_type: String
});

I've tried updating 'networks' with these two snippets of code and neither work. I'm about to pull my hair out.

self.userModel.update({account: key}, {networks: self.client_data[key]['networks']}, function(err) {});

And (note that I've tried adding a callback to save(), and it executes without error)

self.userModel.findOne({account: key}, function(err, doc) {
    doc.networks = self.client_data[key]['networks'];
    doc.markModified('networks').save();
});

Any help would be appreciated! Thanks!

Edit:

The problem was that the object was like so {'some.thing': {more: 'stuff'}} obviously it didn't like the . which is understandable!

like image 768
rickh Avatar asked Mar 28 '12 18:03

rickh


2 Answers

Try doc.markModified('networks');. It looks like networks is a schemaless type. Mongoose can't autodetect changes to schemaless types.

like image 124
Joel Wietelmann Avatar answered Sep 22 '22 23:09

Joel Wietelmann


You need to define your schema fully for this to work. For example:

networks { type : "String" }

like image 30
Conor Avatar answered Sep 22 '22 23:09

Conor