new Admin(){Admin}
Create a new Admin instance (INTERNAL TYPE, do not instantiate directly)
Returns:
collection instance.Methods
-
addUser(username, password, options, callback){Promise}
-
Add a user to the database.
Name Type Default Description username
string The username.
password
string The password.
options
object null optional Optional settings.
Name Type Default Description w
number | string null optional The write concern.
wtimeout
number null optional The write concern timeout.
j
boolean false optional Specify a journal write concern.
fsync
boolean false optional Specify a file sync write concern.
customData
object null optional Custom data associated with the user (only Mongodb 2.6 or higher)
roles
Array.<object> null optional Roles associated with the created user (only Mongodb 2.6 or higher)
callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// An example of how to add a user to the admin database var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin11', 'admin11', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin11', 'admin11', function(err, result) { test.ok(result); adminDb.removeUser('admin11', function(err, result) { test.ok(result); db.close(); }); }); }); }); n example of how to remove a user from the admin database example-class Admin example-method removeUser ignore rts.shouldCorrectlyAddAUserAndRemoveItFromAdminDb = { tadata: { requires: { topology: 'single' } }, The actual test we wish to run st: function(configure, test) { var db = configure.newDbInstance({w:1}, {poolSize:1}); db.open(function(err, db) { var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin12', 'admin12', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin12', 'admin12', function(err, result) { test.ok(result); // Remove the user adminDb.removeUser('admin12', function(err, result) { test.equal(null, err); test.equal(true, result); // Authenticate using the removed user should fail adminDb.authenticate('admin12', 'admin12', function(err, result) { test.ok(err != null); test.ok(!result); db.close(); }); }) }); }); });
// An example of how to add a user to the admin database using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin11', 'admin11').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin11', 'admin11').then(function(result) { test.ok(result); adminDb.removeUser('admin11').then(function(result) { test.ok(result); db.close(); }); }); }); }); n example of how to remove a user from the admin database using a Promise. example-class Admin example-method removeUser ignore rts.shouldCorrectlyAddAUserAndRemoveItFromAdminDbWithPromises = { tadata: { requires: { promises:true, topology: 'single' } }, The actual test we wish to run st: function(configuration, test) { var db = configuration.newDbInstance({w:1}, {poolSize:1}); db.open().then(function(db) { var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin12', 'admin12').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin12', 'admin12').then(function(result) { test.ok(result); // Remove the user adminDb.removeUser('admin12').then(function(result) { test.equal(true, result); // Authenticate using the removed user should fail adminDb.authenticate('admin12', 'admin12').then(function(result) { }).catch(function(err) { db.close(); }); }) }); }); });
// An example of how to add a user to the admin database using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin11', 'admin11'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin11', 'admin11'); test.ok(result); var result = yield adminDb.removeUser('admin11'); test.ok(result); db.close(); }); n example of how to remove a user from the admin database using a Generator and the co module. example-class Admin example-method removeUser ignore rts.shouldCorrectlyAddAUserAndRemoveItFromAdminDbWithGenerators = { tadata: { requires: { generators:true, topology: 'single' } }, The actual test we wish to run st: function(configuration, test) { var co = require('co'); co(function*() { // Connect var db = yield configuration.newDbInstance({w:1}, {poolSize:1}).open(); var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin12', 'admin12'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin12', 'admin12'); test.ok(result); // Remove the user var result = yield adminDb.removeUser('admin12'); test.equal(true, result); try { // Authenticate using the removed user should fail yield adminDb.authenticate('admin12', 'admin12'); } catch(err) { db.close(); } });
-
authenticate(username, password, callback){Promise}
-
Authenticate a user against the server.
Name Type Description username
string The username.
password
string optional The password.
callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// Authenticate against MongoDB Admin user var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w:1}, function(err, doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin2', 'admin2', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin2', 'admin2', function(err, result) { test.ok(result); adminDb.removeUser('admin2', function(err, result) { test.ok(result); db.close(); }); }); }); }); });
// Authenticate against MongoDB Admin user using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test_with_promise'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w:1}).then(function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin2', 'admin2').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin2', 'admin2').then(function(result) { test.ok(result); adminDb.removeUser('admin2').then(function(result) { test.ok(result); db.close(); }); }); }); }); });
// Authenticate against MongoDB Admin user using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Grab a collection object var collection = db.collection('test_with_generators'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted yield collection.insertOne({'a':1}, {w:1}); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin2', 'admin2'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin2', 'admin2'); test.ok(result); var result = yield adminDb.removeUser('admin2') test.ok(result); db.close(); });
-
buildInfo(callback){Promise}
-
Retrieve the server information for the current
instance of the db clientName Type Description callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// Retrieve the buildInfo for the current MongoDB instance var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin3', 'admin3', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin3', 'admin3', function(err, result) { test.ok(result); // Retrive the build information for the MongoDB instance adminDb.buildInfo(function(err, info) { test.ok(err == null); adminDb.removeUser('admin3', function(err, result) { test.ok(result); db.close(); }); }); }); }); });
// Retrieve the buildInfo for the current MongoDB instance using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin3', 'admin3').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin3', 'admin3').then(function(result) { test.ok(result); // Retrive the build information for the MongoDB instance adminDb.buildInfo().then(function(info) { adminDb.removeUser('admin3').then(function(result) { test.ok(result); db.close(); }); }); }); }); });
// Retrieve the buildInfo for the current MongoDB instance using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin3', 'admin3'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin3', 'admin3'); test.ok(result); // Retrive the build information for the MongoDB instance yield adminDb.buildInfo(); var result = yield adminDb.removeUser('admin3'); test.ok(result); db.close(); });
-
command(command, options, callback){Promise}
-
Execute a command
Name Type Default Description command
object The command hash
options
object null optional Optional settings.
Name Type Default Description readPreference
ReadPreference | string null optional The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
maxTimeMS
number null optional Number of milliseconds to wait before aborting the query.
callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// Retrieve the buildInfo using the command function var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin4', 'admin4', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin4', 'admin4', function(err, result) { test.ok(result); // Retrive the build information using the admin command adminDb.command({buildInfo:1}, function(err, info) { test.ok(err == null); adminDb.removeUser('admin4', function(err, result) { test.ok(result); db.close(); }); }); }); }); });
// Retrieve the buildInfo using the command function using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin4', 'admin4').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin4', 'admin4').then(function(result) { test.ok(result); // Retrive the build information using the admin command adminDb.command({buildInfo:1}).then(function(info) { adminDb.removeUser('admin4').then(function(result) { test.ok(result); db.close(); }); }); }); }); });
// Retrieve the buildInfo using the command function using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin4', 'admin4'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin4', 'admin4'); test.ok(result); // Retrive the build information using the admin command yield adminDb.command({buildInfo:1}) var result = yield adminDb.removeUser('admin4'); test.ok(result); db.close(); });
-
listDatabases(callback){Promise}
-
List the available databases
Name Type Description callback
Admin~resultCallback optional The command result callback.
Returns:
Promise if no callback passed
Examples
// An example of listing all available databases. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // List all the available databases adminDb.listDatabases(function(err, dbs) { test.equal(null, err); test.ok(dbs.databases.length > 0); db.close(); }); });
// An example of listing all available databases. using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // List all the available databases adminDb.listDatabases().then(function(dbs) { test.ok(dbs.databases.length > 0); db.close(); }); });
// An example of listing all available databases. using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // List all the available databases var dbs = yield adminDb.listDatabases(); test.ok(dbs.databases.length > 0); db.close(); });
-
logout(callback){Promise}
-
Logout user from server, fire off on all connections and remove all auth info
Name Type Description callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// An example of how add a user, authenticate and logout var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin10', 'admin10', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin10', 'admin10', function(err, result) { test.ok(result); // Logout the user adminDb.logout(function(err, result) { test.equal(true, result); adminDb.removeUser('admin10', function(err, result) { test.ok(result); db.close(); }); }); }); }); });
// An example of how add a user, authenticate and logout using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin10', 'admin10').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin10', 'admin10').then(function(result) { test.ok(result); // Logout the user adminDb.logout().then(function(result) { test.equal(true, result); adminDb.removeUser('admin10').then(function(result) { test.ok(result); db.close(); }); }); }); }); });
// An example of how add a user, authenticate and logout using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin10', 'admin10'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin10', 'admin10') test.ok(result); // Logout the user var result = yield adminDb.logout(); test.equal(true, result); var result = adminDb.removeUser('admin10'); test.ok(result); db.close(); });
-
ping(callback){Promise}
-
Ping the MongoDB server and retrieve results
Name Type Description callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// An example of how to add a user to the admin database var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin9', 'admin9', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin9', 'admin9', function(err, result) { test.ok(result); // Ping the server adminDb.ping(function(err, pingResult) { test.equal(null, err); adminDb.removeUser('admin9', function(err, result) { test.ok(result); db.close(); }); }); }); }); });
// An example of how to add a user to the admin database using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin9', 'admin9').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin9', 'admin9').then(function(result) { test.ok(result); // Ping the server adminDb.ping().then(function(pingResult) { adminDb.removeUser('admin9').then(function(result) { test.ok(result); db.close(); }); }); }); }); });
// An example of how to add a user to the admin database using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin9', 'admin9'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin9', 'admin9'); test.ok(result); // Ping the server yield adminDb.ping(); var result = yield adminDb.removeUser('admin9'); test.ok(result); db.close(); });
-
profilingInfo(callback){Promise}
-
Retrive the current profiling information for MongoDB
Name Type Description callback
Admin~resultCallback optional The command result callback.
Returns:
Promise if no callback passed
Examples
// An example of how to use the profilingInfo
Use this command to pull back the profiling information currently set for Mongodb var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}, function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin7', 'admin7', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin7', 'admin7', function(err, replies) { // Set the profiling level to all adminDb.setProfilingLevel('all', function(err, level) { // Execute a query command collection.find().toArray(function(err, items) { // Turn off profiling adminDb.setProfilingLevel('off', function(err, level) { // Retrive the profiling information adminDb.profilingInfo(function(err, infos) { test.ok(infos.constructor == Array); test.ok(infos.length >= 1); test.ok(infos[0].ts.constructor == Date); test.ok(infos[0].millis.constructor == Number); adminDb.removeUser('admin7', function(err, result) { test.ok(result); db.close(); }); }); }); }); }); }); }); }); });// An example of how to use the profilingInfo using a Promise.
Use this command to pull back the profiling information currently set for Mongodb var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test_with_promise'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}).then(function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin7', 'admin7').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin7', 'admin7').then(function(replies) { // Set the profiling level to all adminDb.setProfilingLevel('all').then(function(level) { // Execute a query command collection.find().toArray().then(function(items) { // Turn off profiling adminDb.setProfilingLevel('off').then(function(level) { // Retrive the profiling information adminDb.profilingInfo().then(function(infos) { test.ok(infos.constructor == Array); test.ok(infos.length >= 1); test.ok(infos[0].ts.constructor == Date); test.ok(infos[0].millis.constructor == Number); adminDb.removeUser('admin7').then(function(result) { test.ok(result); db.close(); }); }); }); }); }); }); }); }); });// An example of how to use the profilingInfo using a Generator and the co module.
Use this command to pull back the profiling information currently set for Mongodb var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Grab a collection object var collection = db.collection('test_with_generators'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted yield collection.insertOne({'a':1}, {w: 1}); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin7', 'admin7'); // Authenticate using the newly added user yield adminDb.authenticate('admin7', 'admin7'); // Set the profiling level to all yield adminDb.setProfilingLevel('all'); // Execute a query command yield collection.find().toArray(); // Turn off profiling yield adminDb.setProfilingLevel('off'); // Retrive the profiling information var infos = yield adminDb.profilingInfo(); test.ok(infos.constructor == Array); test.ok(infos.length >= 1); test.ok(infos[0].ts.constructor == Date); test.ok(infos[0].millis.constructor == Number); var result = yield adminDb.removeUser('admin7'); test.ok(result); db.close(); }); -
profilingLevel(callback){Promise}
-
Retrieve the current profiling Level for MongoDB
Name Type Description callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// Retrieve the current profiling level set for the MongoDB instance var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}, function(err, doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin5', 'admin5', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin5', 'admin5', function(err, replies) { // Retrive the profiling level adminDb.profilingLevel(function(err, level) { adminDb.removeUser('admin5', function(err, result) { test.ok(result); db.close(); }); }); }); }); }); });
// Retrieve the current profiling level set for the MongoDB instance using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test_with_promise'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}).then(function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin5', 'admin5').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin5', 'admin5').then(function(replies) { // Retrive the profiling level adminDb.profilingLevel().then(function(level) { adminDb.removeUser('admin5').then(function(result) { test.ok(result); db.close(); }); }); }); }); }); });
// Retrieve the current profiling level set for the MongoDB instance using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Grab a collection object var collection = db.collection('test_with_generators'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted yield collection.insertOne({'a':1}, {w: 1}); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin5', 'admin5'); // Authenticate using the newly added user yield adminDb.authenticate('admin5', 'admin5'); // Retrive the profiling level yield adminDb.profilingLevel(); var result = yield adminDb.removeUser('admin5'); test.ok(result); db.close(); });
-
removeUser(username, options, callback){Promise}
-
Remove a user from a database
Name Type Default Description username
string The username.
options
object null optional Optional settings.
Name Type Default Description w
number | string null optional The write concern.
wtimeout
number null optional The write concern timeout.
j
boolean false optional Specify a journal write concern.
fsync
boolean false optional Specify a file sync write concern.
callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// An example of how to remove a user from the admin database var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin12', 'admin12', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin12', 'admin12', function(err, result) { test.ok(result); // Remove the user adminDb.removeUser('admin12', function(err, result) { test.equal(null, err); test.equal(true, result); // Authenticate using the removed user should fail adminDb.authenticate('admin12', 'admin12', function(err, result) { test.ok(err != null); test.ok(!result); db.close(); }); }) }); }); });
// An example of how to remove a user from the admin database using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin12', 'admin12').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin12', 'admin12').then(function(result) { test.ok(result); // Remove the user adminDb.removeUser('admin12').then(function(result) { test.equal(true, result); // Authenticate using the removed user should fail adminDb.authenticate('admin12', 'admin12').then(function(result) { }).catch(function(err) { db.close(); }); }) }); }); });
// An example of how to remove a user from the admin database using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin12', 'admin12'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin12', 'admin12'); test.ok(result); // Remove the user var result = yield adminDb.removeUser('admin12'); test.equal(true, result); try { // Authenticate using the removed user should fail yield adminDb.authenticate('admin12', 'admin12'); } catch(err) { db.close(); } });
-
replSetGetStatus(callback){Promise}
-
Get ReplicaSet status
Name Type Description callback
Admin~resultCallback optional The command result callback.
Returns:
Promise if no callback passed
Examples
// Retrieve the current replicaset status if the server is running as part of a replicaset var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}, function(err, doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin14', 'admin14', function(err, result) { test.equal(null, err); test.ok(result != null); // Authenticate using the newly added user adminDb.authenticate('admin14', 'admin14', function(err, result) { test.equal(null, err); test.equal(true, result); // Retrive the server Info, returns error if we are not // running a replicaset adminDb.replSetGetStatus(function(err, info) { adminDb.removeUser('admin14', function(err, result) { test.equal(null, err); test.ok(result); db.close(); }); }) }); }); }); });
// Retrieve the current replicaset status if the server is running as part of a replicaset using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Grab a collection object var collection = db.collection('test_with_generators'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted yield collection.insertOne({'a':1}, {w: 1}); // Use the admin database for the operation var adminDb = db.admin(); // Retrive the server Info, returns error if we are not // running a replicaset yield adminDb.replSetGetStatus(); db.close(); });
// Retrieve the current replicaset status if the server is running as part of a replicaset using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test_with_promise'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}).then(function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin14', 'admin14').then(function(result) { test.ok(result != null); // Authenticate using the newly added user adminDb.authenticate('admin14', 'admin14', {w:1}).then(function(result) { test.equal(true, result); // Retrive the server Info, returns error if we are not // running a replicaset adminDb.replSetGetStatus().then(function(info) { adminDb.removeUser('admin14').then(function(result) { test.ok(result); db.close(); }); }).catch(function(err) { // // console.dir(err) }); }).catch(function(err) { // // console.dir(err) }); }); }); });
-
serverInfo(callback){Promise}
-
Retrieve the server information for the current
instance of the db clientName Type Description callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
-
serverStatus(callback){Promise}
-
Retrieve this db's server status.
Name Type Description callback
Admin~resultCallback optional The command result callback
Returns:
Promise if no callback passed
Examples
// Retrieve the current server Info var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}, function(err, doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin13', 'admin13', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin13', 'admin13', function(err, result) { // Retrive the server Info adminDb.serverStatus(function(err, info) { test.equal(null, err); test.ok(info != null); adminDb.removeUser('admin13', function(err, result) { test.ok(result); db.close(); }); }); }); }); }); });
// Retrieve the current server Info using a Promise. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test_with_promise'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}).then(function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin13', 'admin13').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin13', 'admin13').then(function(result) { // Retrive the server Info adminDb.serverStatus().then(function(info) { test.ok(info != null); adminDb.removeUser('admin13').then(function(result) { test.ok(result); db.close(); }); }); }); }); }); });
// Retrieve the current server Info using a Generator and the co module. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Grab a collection object var collection = db.collection('test_with_generators'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted yield collection.insertOne({'a':1}, {w: 1}); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin13', 'admin13'); // Authenticate using the newly added user yield adminDb.authenticate('admin13', 'admin13'); // Retrive the server Info var info = yield adminDb.serverStatus(); test.ok(info != null); var result = yield adminDb.removeUser('admin13'); test.ok(result); db.close(); });
-
setProfilingLevel(level, callback){Promise}
-
Set the current profiling level of MongoDB
Name Type Description level
string The new profiling level (off, slow_only, all).
callback
Admin~resultCallback optional The command result callback.
Returns:
Promise if no callback passed
Examples
// An example of how to use the setProfilingInfo
Use this command to set the Profiling level on the MongoDB server var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}, function(err, doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin6', 'admin6', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin6', 'admin6', function(err, replies) { // Set the profiling level to only profile slow queries adminDb.setProfilingLevel('slow_only', function(err, level) { // Retrive the profiling level and verify that it's set to slow_only adminDb.profilingLevel(function(err, level) { test.equal('slow_only', level); // Turn profiling off adminDb.setProfilingLevel('off', function(err, level) { // Retrive the profiling level and verify that it's set to off adminDb.profilingLevel(function(err, level) { test.equal('off', level); // Set the profiling level to log all queries adminDb.setProfilingLevel('all', function(err, level) { // Retrive the profiling level and verify that it's set to all adminDb.profilingLevel(function(err, level) { test.equal('all', level); // Attempt to set an illegal profiling level adminDb.setProfilingLevel('medium', function(err, level) { test.ok(err instanceof Error); test.equal("Error: illegal profiling level value medium", err.message); adminDb.removeUser('admin6', function(err, result) { test.ok(result); db.close(); }); }); }) }); }) }); }) }); }); }); }); });// An example of how to use the setProfilingInfo using a Promise.
Use this command to set the Profiling level on the MongoDB server var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test_with_promise'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}).then(function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin6', 'admin6').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin6', 'admin6').then(function(replies) { // Set the profiling level to only profile slow queries adminDb.setProfilingLevel('slow_only').then(function(level) { // Retrive the profiling level and verify that it's set to slow_only adminDb.profilingLevel().then(function(level) { test.equal('slow_only', level); // Turn profiling off adminDb.setProfilingLevel('off').then(function(level) { // Retrive the profiling level and verify that it's set to off adminDb.profilingLevel().then(function(level) { test.equal('off', level); // Set the profiling level to log all queries adminDb.setProfilingLevel('all').then(function(level) { // Retrive the profiling level and verify that it's set to all adminDb.profilingLevel().then(function(level) { test.equal('all', level); // Attempt to set an illegal profiling level adminDb.setProfilingLevel('medium').then(function(level) { }).catch(function(err) { test.ok(err instanceof Error); test.equal("Error: illegal profiling level value medium", err.message); adminDb.removeUser('admin6').then(function(result) { test.ok(result); db.close(); }); }); }) }); }) }); }) }); }); }); }); });// An example of how to use the setProfilingInfo using a Generator and the co module.
Use this command to set the Profiling level on the MongoDB server var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Grab a collection object var collection = db.collection('test_with_generators'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted yield collection.insertOne({'a':1}, {w: 1}); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin6', 'admin6'); // Authenticate using the newly added user yield adminDb.authenticate('admin6', 'admin6'); // Set the profiling level to only profile slow queries yield adminDb.setProfilingLevel('slow_only') // Retrive the profiling level and verify that it's set to slow_only var level = yield adminDb.profilingLevel(); test.equal('slow_only', level); // Turn profiling off yield adminDb.setProfilingLevel('off'); // Retrive the profiling level and verify that it's set to off var level = yield adminDb.profilingLevel(); test.equal('off', level); // Set the profiling level to log all queries yield adminDb.setProfilingLevel('all'); // Retrive the profiling level and verify that it's set to all var level = yield adminDb.profilingLevel(); test.equal('all', level); try { // Attempt to set an illegal profiling level yield adminDb.setProfilingLevel('medium'); } catch(err) { test.ok(err instanceof Error); test.equal("Error: illegal profiling level value medium", err.message); var result = yield adminDb.removeUser('admin6'); test.ok(result); db.close(); } }); -
validateCollection(collectionName, options, callback){Promise}
-
Validate an existing collection
Name Type Default Description collectionName
string The name of the collection to validate.
options
object null optional Optional settings.
callback
Admin~resultCallback optional The command result callback.
Returns:
Promise if no callback passed
Examples
// An example of how to use the validateCollection command
Use this command to check that a collection is valid (not corrupt) and to get various statistics. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}, function(err, doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin8', 'admin8', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin8', 'admin8', function(err, replies) { // Validate the 'test' collection adminDb.validateCollection('test', function(err, doc) { test.equal(null, err); adminDb.removeUser('admin8', function(err, result) { test.ok(result); db.close(); }); }); }); }); }); }); n example of how to add a user to the admin database example-class Admin example-method ping ignore rts.shouldCorrectlyPingTheMongoDbInstance = { tadata: { requires: { topology: 'single' } }, The actual test we wish to run st: function(configure, test) { var db = configure.newDbInstance({w:1}, {poolSize:1}); db.open(function(err, db) { var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin9', 'admin9', function(err, result) { // Authenticate using the newly added user adminDb.authenticate('admin9', 'admin9', function(err, result) { test.ok(result); // Ping the server adminDb.ping(function(err, pingResult) { test.equal(null, err); adminDb.removeUser('admin9', function(err, result) { test.ok(result); db.close(); }); }); }); }); });// An example of how to use the validateCollection command using a Promise.
Use this command to check that a collection is valid (not corrupt) and to get various statistics. var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Grab a collection object var collection = db.collection('test_with_promise'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted collection.insertOne({'a':1}, {w: 1}).then(function(doc) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin8', 'admin8').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin8', 'admin8').then(function(replies) { // Validate the 'test' collection adminDb.validateCollection('test_with_promise').then(function(doc) { // Remove the user adminDb.removeUser('admin8').then(function(result) { test.ok(result); db.close(); }); }); }); }); }); }); n example of how to add a user to the admin database using a Promise. example-class Admin example-method ping ignore rts.shouldCorrectlyPingTheMongoDbInstanceWithPromises = { tadata: { requires: { promises:true, topology: 'single' } }, The actual test we wish to run st: function(configuration, test) { var db = configuration.newDbInstance({w:1}, {poolSize:1}); db.open().then(function(db) { var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database adminDb.addUser('admin9', 'admin9').then(function(result) { // Authenticate using the newly added user adminDb.authenticate('admin9', 'admin9').then(function(result) { test.ok(result); // Ping the server adminDb.ping().then(function(pingResult) { adminDb.removeUser('admin9').then(function(result) { test.ok(result); db.close(); }); }); }); }); });// An example of how to use the validateCollection command using a Generator and the co module.
Use this command to check that a collection is valid (not corrupt) and to get various statistics. var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Grab a collection object var collection = db.collection('test_with_generators'); // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted yield collection.insertOne({'a':1}, {w: 1}); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin8', 'admin8'); // Authenticate using the newly added user yield adminDb.authenticate('admin8', 'admin8'); // Validate the 'test' collection var doc = yield adminDb.validateCollection('test_with_generators'); test.ok(doc != null); var result = yield adminDb.removeUser('admin8') test.ok(result); db.close(); }); n example of how to add a user to the admin database using a Generator and the co module. example-class Admin example-method ping ignore rts.shouldCorrectlyPingTheMongoDbInstanceWithGenerators = { tadata: { requires: { generators:true, topology: 'single' } }, The actual test we wish to run st: function(configuration, test) { var co = require('co'); co(function*() { // Connect var db = yield configuration.newDbInstance({w:1}, {poolSize:1}).open(); var MongoClient = require('mongodb').MongoClient, co = require('co'); test = require('assert'); co(function*() { var db = yield MongoClient.connect('mongodb://localhost:27017/test'); // Use the admin database for the operation var adminDb = db.admin(); // Add the new user to the admin database yield adminDb.addUser('admin9', 'admin9'); // Authenticate using the newly added user var result = yield adminDb.authenticate('admin9', 'admin9'); test.ok(result); // Ping the server yield adminDb.ping(); var result = yield adminDb.removeUser('admin9'); test.ok(result); db.close(); });
Type Definitions
-
resultCallback(error, result)
-
The callback format for results
Name Type Description error
MongoError An error instance representing the error during the execution.
result
object The result object if the command was executed successfully.