Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose save() not updating value in an array in database document

I am trying to update a document in a collection (units) using GUI and after it gets updated I want to update the value (user.Units which is an array of Unit names) in collection (users). If the array length is just 1 element it gets updated and also shows up in database and everything works well, but when Array of Units have more than one element , I try to update it through a for loop, it shows it gets updated but when I check the database it is still not updated.

I really can't figure out why its not updating the database when I update the value through a loop.

Whole Edit and update function:-

 edit_unit: function (req, res, next) {     var Data = req.body;      Client_data.Unit.findById(req.params.unitId, function (err, unit) {         var error = false;         if (err) {             error = err;         } else if (!unit) {             error = "FATAL: unable to look up Unit #" + req.params.unitId;         } else {              switch(req.body.name) {                 case 'Icon':                     var Icon = unit.Icon;                      User.find({"Units":Icon}, function (err, users) {                         if (err)                         console.log(err);                          users.forEach(function (u) {                             if (u.Units.length > 1) {                             for (var i = 0; i <= u.Units.length; i++) {                                if(u.Units[i] == Icon) {                                    u.Units[i] = req.body.value;                                }                             }                             }                             else {                                 u.Units = req.body.value;                             }                             u.save(u);                         });                     });                     unit[req.body.name] = req.body.value;                     break;                 case 'Description':                     unit[req.body.name] = req.body.value;                     break;                 default:                     unit[req.body.name] = req.body.value;                     break;             }             var data = JSON.stringify(req.body);             unit.save();              res.writeHead(200, {                 'Content-Length': data.length,                 'Content-Type':  'application/json'             });             res.end(data);         }     }); } 

req.body:-

{ name: 'Icon',   value: 'Health Utility 22c',   pk: '5395ed107cd92dc40eaafb56'  } 

User Schema:-

var userSchema = mongoose.Schema({ UserName:     { type: String, required: true }, Password:     { type: String }, FirstName:    { type: String, required: true }, LastName:     { type: String, required: true }, CompanyName:  { type: String }, PhoneNumber:  { type: Number }, StartDate:    { type: Date,   required: true }, EndDate:      { type: Date,   required: true, default: new Date('9999-12-12')  }, ClientID:     { type: ObjectId, ref: 'Client', default: null }, DepartmentID: { type: ObjectId, ref: 'Department' }, ManagerID:    { type: ObjectId, ref: 'User', default: null}, Units:        [ { type: String, required: true } ], UserList:      { type: Array, default:[]}, Access:    [{ type: String, enum: ['DEMO', 'USER','MANAGER','ADMINISTRATOR','OWNER']}], Credentials:  { type: String }, AFTE:         { type: Number}, SessionID:    { type: String, default: null } }, { safe: true }); 
like image 545
Suleman Mirza Avatar asked Jul 07 '14 19:07

Suleman Mirza


People also ask

What does save () do in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

Does Mongoose save overwrite?

Mongoose save with an existing document will not override the same object reference. Bookmark this question.

What is difference between save and create in Mongoose?

create() is a shortcut for saving one or more documents to the database. MyModel. create(docs) does new MyModel(doc). save() for every doc in docs.

Does update call save in Mongoose?

When you create an instance of a Mongoose model using new , calling save() makes Mongoose insert a new document. If you load an existing document from the database and modify it, save() updates the existing document instead.


1 Answers

Maybe notify mongooose the dataset has changed like this :

doc.markModified('pathToYourAttribute')  

From the docs http://mongoosejs.com/docs/schematypes.html

person.anything = { x: [3, 4, { y: "changed" }] };  person.markModified('anything'); 

Hope it helps!

like image 99
cvng Avatar answered Oct 09 '22 01:10

cvng