Skip to content

Commit eb79b5a

Browse files
kmaharmbroadst
authored andcommitted
feat(listDatabases): add support for nameOnly option to listDatabases
NODE-1129
1 parent fc9bea4 commit eb79b5a

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

lib/admin.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
var toError = require('./utils').toError,
44
Define = require('./metadata'),
5-
shallowClone = require('./utils').shallowClone;
5+
shallowClone = require('./utils').shallowClone,
6+
assign = require('./utils').assign;
67

78
/**
89
* @fileOverview The **Admin** class is an internal class that allows convenient access to
@@ -358,18 +359,31 @@ define.classMethod('validateCollection', { callback: true, promise: true });
358359
/**
359360
* List the available databases
360361
*
362+
* @param {object} [options=null] Optional settings.
363+
* @param {boolean} [options.nameOnly=false] Whether the command should return only db names, or names and size info.
361364
* @param {Admin~resultCallback} [callback] The command result callback.
362365
* @return {Promise} returns Promise if no callback passed
363366
*/
364-
Admin.prototype.listDatabases = function(callback) {
367+
Admin.prototype.listDatabases = function(options, callback) {
365368
var self = this;
369+
370+
var cmd = { listDatabases: 1 };
371+
372+
// no options were passed in, only a callback
373+
if (typeof options == 'function') return self.s.db.executeDbAdminCommand(cmd, {}, options);
374+
375+
// if we did get options, use them
376+
cmd = assign(cmd, options);
377+
378+
// cast boolean to 1 or 0
379+
if (cmd.nameOnly) cmd.nameOnly = Number(cmd.nameOnly);
380+
366381
// Execute using callback
367-
if (typeof callback == 'function')
368-
return self.s.db.executeDbAdminCommand({ listDatabases: 1 }, {}, callback);
382+
if (typeof callback == 'function') return self.s.db.executeDbAdminCommand(cmd, {}, callback);
369383

370384
// Return a Promise
371385
return new this.s.promiseLibrary(function(resolve, reject) {
372-
self.s.db.executeDbAdminCommand({ listDatabases: 1 }, {}, function(err, r) {
386+
self.s.db.executeDbAdminCommand(cmd, {}, function(err, r) {
373387
if (err) return reject(err);
374388
resolve(r);
375389
});

test/functional/operation_example_tests.js

+49-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var test = require('./shared').assert;
33
var setupDatabase = require('./shared').setupDatabase;
44
var f = require('util').format;
5+
var expect = require('chai').expect;
56

67
describe('Operation Examples', function() {
78
before(function() {
@@ -4822,7 +4823,7 @@ describe('Operation Examples', function() {
48224823
* @example-method dropDatabase
48234824
* @ignore
48244825
*/
4825-
it('shouldCorrectlyDropTheDatabase', {
4826+
it('should correctly drop the database', {
48264827
metadata: { requires: { topology: ['single'] } },
48274828

48284829
// The actual test we wish to run
@@ -5436,7 +5437,7 @@ describe('Operation Examples', function() {
54365437
* @example-method listDatabases
54375438
* @ignore
54385439
*/
5439-
it('shouldCorrectlyListAllAvailableDatabases', {
5440+
it('should correctly list all available databases', {
54405441
metadata: {
54415442
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
54425443
},
@@ -5472,6 +5473,52 @@ describe('Operation Examples', function() {
54725473
}
54735474
});
54745475

5476+
it('should correctly list all available databases names and no database sizes', {
5477+
metadata: {
5478+
requires: {
5479+
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'],
5480+
mongodb: '>=3.2.13'
5481+
}
5482+
},
5483+
5484+
// The actual test we wish to run
5485+
test: function(done) {
5486+
var configuration = this.configuration;
5487+
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
5488+
client.connect(function(err, client) {
5489+
// LINE var MongoClient = require('mongodb').MongoClient,
5490+
// LINE test = require('assert');
5491+
// LINE MongoClient.connect('mongodb://localhost:27017/test', function(err, client) {
5492+
// LINE var db = client.db('test);
5493+
// REPLACE configuration.writeConcernMax() WITH {w:1}
5494+
// REMOVE-LINE restartAndDone
5495+
// REMOVE-LINE done();
5496+
// REMOVE-LINE var db = client.db(configuration.db);
5497+
// BEGIN
5498+
var db = client.db(configuration.db);
5499+
// Use the admin database for the operation
5500+
var adminDb = db.admin();
5501+
5502+
// List all the available databases
5503+
adminDb.listDatabases({ nameOnly: 1 }, function(err, dbs) {
5504+
expect(err).to.not.exist;
5505+
expect(dbs.databases).to.include.deep.members([
5506+
{
5507+
name: 'admin'
5508+
},
5509+
{
5510+
name: 'local'
5511+
}
5512+
]);
5513+
5514+
client.close();
5515+
done();
5516+
});
5517+
});
5518+
// END
5519+
}
5520+
});
5521+
54755522
/**
54765523
* Retrieve the current server Info
54775524
*

0 commit comments

Comments
 (0)