Why do you use mongoose you say?
Let’s face it, writing MongoDB validation, casting and business logic boilerplate is a drag. Mongoose eases the whole mess.
While working with mongoose specially on the update operation , you will notice that you will have to provide the selector for the document you are modifying (normally _id or username ) along with the fields you wish to modify and their corresponding values that you would like to update.
Here is a simple snippet to summarize the whole thought above
User.update({
"username": req.params.user} , {$set:
{age: req.body.age, location: req.body.location, name:req.body.name ....}
} ,
function (err , success) {
if (err) throw (err);
else {
res.send({
msg: 'update success'
})
}})
The code is okay if you are to update two to handful of fields but its get kinda ugly as the field to be updated gets increased. Say 100 fields . Also if you do manage to write the update query for 100 fields , whats the chance that the properties names in the schema will remain the same forever. If the schema were to update, you will be writing 100+100 = 200 update assignments.
This is where my code comes to rescue. Not only it dynamically populates the update fields on the query , it also picks up the field name from the request.
const entries = Object.keys(req.body)
const updates = {}
// constructing dynamic query
for (let i = 0; i < entries.length; i++) {
updates[entries[i]] = Object.values(req.body)[i]
}
User.update({
"username": req.params.user
} , {
$set: updates
} ,
function (err , success) {
if (err) throw (err);
else {
res.send({
msg: "update success"
})
}
}
Happy Node'ing
Top comments (6)
Thanks!
can't we just use
???
Awesome.. Thank you.. 🥇
anytime
Awesome THnaks
Nice
I was searching for such solution .. thanks for sharing