I'm having issues updating a doc through Node + Express and Mongodb via Mongoskin. I used the following via the mongo command line and it works as expected:
db.userlist.update({_id: ObjectId('5377821219f21e974150bacf')}, {$set: {username: "Test"}})
However doing the similar line within Node doesn't work, and seems to return a 500 error via browser.
db.collection('userlist').update({_id: ObjectId('5377821219f21e974150bacf')}, {$set: {username: "Test"}});
I've tried passing the options multi: true and false and also tried adding a callback, but every time it doesn't work.
What am I missing? p.s. I'm very new to node and mongodb, thanks in advance.
Well your syntax is off - the syntax for the mongo shell is not the same for calling functions in node.js via mongoskin. I've updated your code to what I believe will work based on this tutorial: http://www.hacksparrow.com/mongoskin-tutorial-with-examples.html :
var mongo = require('mongoskin');
require('mongodb');
var db = mongo.db("mongodb://localhost:27017/mongoskin", {native_parser:true});
db.collection('userlist').update({_id: mongo.helper.toObjectID("5377821219f21e974150bacf")}, {'$set':{username:"Test"}}, function(err, result) {
if (err) throw err;
if (result) console.log('Updated!');
});
Note the mongo.helper.toObjectID(). From the documentation https://github.com/kissjs/node-mongoskin :
collection.update({_id: toObjectID(id)}, ...)