Mongoose does not update inline document

I have a problem when I try to update an embedded document in mongodb. I tried two methods and did not work, and I searched everywhere for reasons why this is not being updated. In any case, my schema looks like this (I can notice 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 tried updating the "network" with these two pieces of code and did not work. I'm going to pull my hair out.

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

And (note that I tried adding a save () callback and it runs without errors)

 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 {'some.thing': {more: 'stuff'}} , obviously he didn’t like it. which is understandable!

+6
source share
2 answers

Try doc.markModified('networks'); . networks seem to be a type of circuit. Mongoose cannot automatically detect schema type changes.

+11
source

You need to fully define your circuit for this to work. For instance:

network {type: "String"}

+2
source

Source: https://habr.com/ru/post/911879/


All Articles