public
Last active

node.js list all mongodb databases

  • Download Gist
action.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**
* 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 */
action.sh
Shell
1
$ node script.js
install.sh
Shell
1
$ npm install mongoose
mongodb_list.js
JavaScript
1 2 3 4
var mongoose = require('mongoose')
, dbNative = mongoose.connection.db;
 
console.log(dbNative);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.