Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 598 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mongoose save() not updating value in an array in database document

#1
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 });


Reply

#2
Refer to [this issue][1]. One way to solve this would be not to update your array via the classic array Index method. So do this

doc.array.set(index, value);

Instead of

doc.array[index] = value;
also view the [FAQ][2] and [doc][3] for more details.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
Maybe notify mongooose the dataset has changed like this :

doc.markModified('pathToYourAttribute') 

From the docs [

[To see links please register here]

](

[To see links please register here]

)

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

Hope it helps!
Reply

#4
I replaced the save method with this statement:
```
Client_data.Unit.updateOne({_id: unit._id},unit);
Reply

#5
yep, I solved this issue by adding doc.markModified('fullName'); etc
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through