node.js - MongoDB collection.save() duplicating objects -
i'm working on first web dev project involving backend work, , i'm giving mongodb shot. i'm working on simple admin panel every user listed, , clicking on user admin can go in , manually edit info, update, , return master user list see changes.
i'm trying use collection.save()
method update existing users, i've encountered problem where, instead of updating, creates duplicate copy of document matching _id
number. here image illustrating mean, , here of code update:
router.post('/updateuser', function(req, res) { var db = req.db; var collection = req.collection; var userid = req.body.userid; var djstatus = req.body.djstatus; var access = req.body.access; var firstname = req.body.firstname; var lastname = req.body.lastname; var email = req.body.email; var phone = req.body.phone; var studentstatus = req.body.studentstatus; var macidnum = req.body.macidnum; var iclass = req.body.iclass; var gradyear = req.body.gradyear; var show = req.body.show; var blurb = req.body.blurb; collection.save( { _id: userid, djstatus: djstatus, access: access, firstname: firstname, lastname: lastname, email: email, phone: phone, studentstatus: studentstatus, macidnum: macidnum, iclass: iclass, gradyear: gradyear, show: show, blurb: blurb }, function (err, doc) { if (err) { res.send('there problem updating'); } else { console.log(doc + ' doc'); res.location('admin/users'); res.redirect('admin/users'); } });
});
i not sure why happening. appreciated!
from post, i'm not sure mongodb driver you're using save() not used updating document in mongodb. there should update() function mongodb driver using. example, mongoskin driver, syntax insert/update:
collection.update(_id: userid, {$set: data}, {safe:true, upsert:true}, function (err, result) { // check err , result , });
Comments
Post a Comment