Node.js will not close application while there is some events subscribed and potential logic can happen.
When you create MongoClient it creates EventEmitter as well and it will not let node.js process to exit as it potentially can receive some events.
If you want to get cursor back - then you have few options:
- Nicely kill all EventEmitters and idling timers and intervals and process will then exit. But it is hard to achieve.
- Just call:
process.exit(0)
which will nicely close process.
As well check ref
and unref
for timer functions (interval, timeout): http://nodejs.org/api/timers.html#timers_unref
In case with MongoClient, just close database connection when you are done with it:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/testdb', function(err, db) {
// do your stuff
// when you are done with database, make sure to respect async queries:
db.close();
});