Skip to content

Instantly share code, notes, and snippets.

@manishtpatel
Created April 19, 2012 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manishtpatel/2423295 to your computer and use it in GitHub Desktop.
Save manishtpatel/2423295 to your computer and use it in GitHub Desktop.
findandmodify issue
var mongodb = require('mongodb'),
request = true;
var db = new mongodb.Db('test_db', new mongodb.Server("127.0.0.1", 27017, {
auto_reconnect: true
}), {});
// listen on error
db.on("error", function(err) {
console.log('open request ', request);
console.error('db on error');
console.dir(err);
});
// open connection
db.open(function(err, client) {
if (err) {
console.error(err);
}
var collection = new mongodb.Collection(client, 'test_collection');
// define find and modify
var findAndModifyLoop = function() {
// mark request = true as sending mongo request
request = true;
console.log('findAndModify request (should not be last)');
collection.findAndModify({
hello: 'world'
}, // query
[
['_id', 'asc']
], // sort order
{
$set: {
hi: 'there'
}
}, // replacement, replaces only the field "hi"
{}, // options
function(err, object) {
if (err) {
console.warn('findAndModify response ', err.message); // returns error if no matching object found
} else {
console.log('findAndModify response', object);
}
// no more out standing request
request = false;
// on result does it again
findAndModifyLoop();
});
};
// start the loop
findAndModifyLoop();
});
// db.on("close", function(err) {
// console.log('open request ', request);
// console.error('db on close');
// console.dir(err);
// });
@manishtpatel
Copy link
Author

Run this js file
While running issue 'net stop mongodb'

I am seeing two results, either case i consider error case
1> It throws an error
2> Its quiting without findAndModify calling callback

OS: windows 7 ultimate, sp1, x64
Mongodb: v 2.0.4, windows x64 version, running as service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment