Node - MongoDB: the collection does not find anything using the mongodb driver. Shell returns a value

I have simple code that is trying to find a document in the MongoDB collection. I can usually find it with the Mongo shell client, but through Node it is impossible, I tried many ways to no avail.

First my results from MongoDB itself:

raul@rmedina:~$ mongo sdk_back
MongoDB shell version: 2.0.6
connecting to: sdk_back
> db.metadatos.find();
{ "_id" : ObjectId("514cbee35c10db0299c015c7"), "tipo" : "proceso" }
> db.metadatos.findOne({"tipo":"proceso"});
{ "_id" : ObjectId("514cbee35c10db0299c015c7"), "tipo" : "proceso" }
> db.metadatos.findOne({tipo:"proceso"});
{ "_id" : ObjectId("514cbee35c10db0299c015c7"), "tipo" : "proceso" }
> 

As you can see, searches and findOne work. Now this is my Node code:

    var utils   = require('../utils/utils.js'),
    Server = require('mongodb').Server,
    Db = require('mongodb').Db;

    exports.procesaJSON = function (input_json){
    if(!utils.validaJSON(input_json))
        throw new Error('JSON de entrada inválido!');

    //procesamos
    input_json = JSON.parse(input_json);

    if(typeof(input_json.id) === "undefined")
        throw new Error('JSON de entrada inválido (falta propiedad \"id\"!');

    //obtenemos meta-data de mongo
    var db = new Db("sdk_back", new Server('localhost',27017,{auto_reconnect:true, poolsize:1}),{safe:true});
    db.open(function(err,db){
        if(!err){
            db.collection("metadatos",function(err,collection){
                console.log('Entering collection meta-data');
                if(!err){
                    console.log('Lets find one');
                    collection.findOne({"tipo":"proceso"},function(err,doc){
                        console.log('Results of findOne');
                        if(!err){
                            console.log(doc);
                        }
                        else{
                            throw new Error('Error al buscar meta_data!');
                        }

                        if(doc){
                            console.log('We have results');
                        }
                        else{
                            console.log('We dont have results');
                        }
                    });
                }
                else{
                    throw new Error('Error al buscar meta_data 1!');
                }
            });
        }
        else{
            throw new Error('Error al conectarse a MongoDB!');
        }
        db.close();
    });
};

And the output is always:

raul@rmedina:~$ node pu_entrypoint.js 
Entering collection meta-data
Lets find one
raul@rmedina:~$ 

As you can see, a log with the text “findOne Results” is never displayed, so the findOne method actually never executes, or something like that.

My question is: what am I doing wrong? I dropped and recreated the collection, dumped the database, and even changed the / data / db directory for Mongo, but to no avail.

What am I doing wrong?

Thank!

[SOLVED] UPDATE: db.close(). Node beign , db , . db.close , . :

db.open(function(err,db){
            if(!err){
                db.collection("metadatos",function(err,collection){
                    console.log('Entering collection meta-data');
                    if(!err){
                        console.log('Lets find one');
                        collection.findOne({"tipo":"proceso"},function(err,doc){
                            console.log('Results of findOne');
                            if(!err){
                                if(doc){
                                    console.log('We have results');
                                    console.log(doc._id);
                                }
                                else{
                                    console.log('We dont have results');
                                }
/********** MOVED db.close() HERE ********************/
                                db.close();
                            }
                            else{
                                throw new Error('Error al buscar meta_data!');
                            }
                        });
                    }
                    else{
                        throw new Error('Error al buscar meta_data 1!');
                    }
                });
            }
            else{
                throw new Error('Error al conectarse a MongoDB!');
            }
/********** REMOVED db.close() FROM HERE ********************/
        });
+5
1

db.close(). Node db, , . db.close . :

    db.open(function(err,db){
        if(!err){
            db.collection("metadatos",function(err,collection){
                console.log('Entering collection meta-data');
                if(!err){
                    console.log('Lets find one');
                    collection.findOne({"tipo":"proceso"},function(err,doc){
                        console.log('Results of findOne');
                        if(!err){
                            if(doc){
                                console.log('We have results');
                                console.log(doc._id);
                            }
                            else{
                                console.log('We dont have results');
                            }
/********** MOVE db.close() HERE ********************/
                            db.close();
                        }
                        else{
                            throw new Error('Error al buscar meta_data!');
                        }
                    });
                }
                else{
                    throw new Error('Error al buscar meta_data 1!');
                }
            });
        }
        else{
            throw new Error('Error al conectarse a MongoDB!');
        }
/********** REMOVED db.close() FROM HERE ********************/
    });
0

All Articles