Skip to content

Instantly share code, notes, and snippets.

@adaptivedev
Created April 3, 2014 14:08
Show Gist options
  • Save adaptivedev/9955033 to your computer and use it in GitHub Desktop.
Save adaptivedev/9955033 to your computer and use it in GitHub Desktop.
trying to use findAndModify from node.js to mongo: problem: no record inserted, null returned
// no such record insert
> use test
switched to db test
> show collections
system.indexes
test
> db.test.find()
{ "_id" : ObjectId("533d5ec091d39080c18ec8ba"), "_accountId" : "100000057408993" }
>
users.js: addUser: mongo example
'addUser: returns: null'
exports.addUser = function(req, res) {
console.log("users.js: addUser: mongo example");
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
db.collection('test').findAndModify({hello: 'world'}, [['_id','asc']], {$set: {hi: 'there'}}, {}, function(err, object) {
if (err) console.warn(err.message);
else console.dir("addUser: returns: " + object); // undefined if no matching object exists.
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment