Created
May 25, 2012 02:48
-
-
Save edwardhotchkiss/2785463 to your computer and use it in GitHub Desktop.
node.js list all mongodb databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Uses MongooseJS to Connect to MongoDB | |
* .Maps out all collections within | |
*/ | |
var mongoose = require('mongoose') | |
, MONGO_DB = 'mongodb://localhost/test'; | |
mongoose.connect(MONGO_DB); | |
mongoose.connection.on('open', function(){ | |
mongoose.connection.db.collectionNames(function(error, names) { | |
if (error) { | |
throw new Error(error); | |
} else { | |
names.map(function(name) { | |
console.log('found collection %s', name); | |
}); | |
} | |
}); | |
}); | |
mongoose.connection.on('error', function(error){ | |
throw new Error(error); | |
}); | |
/* EOF */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node script.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install mongoose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose') | |
, dbNative = mongoose.connection.db; | |
console.log(dbNative); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment