Question

My code looks like this:

#!/bin/env node

var collection = require('mongojs')('test').collection('test');

collection.findOne({}, function(err, doc) {
    console.log(err, doc);
});

When I ran this script, it showed:

$ node test.js
null null

But the script didn't quit. I need to "CTRL+C" to quit it. Any body know how to fix it?

[update]

I found that if I use native mongodb instead of mongojs, there's no problem:

#!/bin/env node

var client = require('mongodb');

client.connect('mongodb://127.0.0.1:27017/hatch', function(err, db) {
    if (err) throw err;
    var collection = db.collection('documents');

    collection.find().toArray(function(err, results) {
        console.dir(results);
        db.close();
    });
});

So is it a mongojs issue?

Was it helpful?

Solution

You have to close the db connection

var mongojs = require('mongojs');
var db = mongojs('test');
var collection = db.collection('test');

collection.findOne({}, function(err, doc) {
    console.log(err, doc);
    db.close();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top