Mongodb + nodejs mongoskin update not working

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.

  • Handling conflict in find, modify, save flow in MongoDB with Mongoose
  • I can't test if mongoose returned a document. Console.log() might be causing problems
  • What's the use of Jade or Handlebars when writing AngularJs apps
  • NodeJS mongoClient invalid operator: $eq
  • Check for Existing Document in Mongo
  • Searching Embedded Objects in Mongoose
  • 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.

  • Why does Mongoose add blank arrays?
  • TypeError: object is not a function in using MongoDB with Node.js
  • Multiple populates - mongoosejs
  • findOne works but not get all/find
  • Determine if a record “is new” in the pre save callback
  • trouble using promises with mongoose
  • One Solution collect form web for “Mongodb + nodejs mongoskin update not working”

    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: :

    var mongo = require('mongoskin');
    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 :

    collection.update({_id: toObjectID(id)}, ...)
    Node.js is the Best Javascript runtime in the world.