Is there a way to check if a database exists in Mongo from the Node.js driver?

I find it unexpectedly hard to find a way to see if a database exists in MongoDB from the Node.js. driver There is no way in the Node.js driver to check if a database exists.

For example, the following does not cause an error:

var mongo = require('mongodb').MongoClient;

mongo.connect({ 'mongodb://localhost:27017/databaseThatDoesntExists }, function (err, db) {
   // There is no error
   if (err) console.log(err);
   // Let get some stats on a database that doesn't exist
   db.statsAsync(function (err, result) {
      console.log(result);
   });
});

The result will be such an object:

{ db: 'databaseThatDoesntExist',
  collections: 0,
  objects: 0,
  avgObjSize: 0,
  dataSize: 0,
  storageSize: 0,
  numExtents: 0,
  indexes: 0,
  indexSize: 0,
  fileSize: 0,
  ok: 1 }

Can you check if the database exists in MongoDB from the Node.js driver? Is there even a database concept existing in MongoDB? Is a link to a database that does not exist, just referring to a database without collections?

Mongo, why can't you just throw a mistake! I like it when my throwserror code !

+4
source share
2

, , . , Mongo.

show dbs

, :

db

, :

use <database>
+1

All Articles