Allows the user to access the admin functionality of MongoDB
- class Admin()¶
Arguments:
- db (object) – Current db instance we wish to perform Admin operations on.
Returns: function Constructor for Admin type.
Retrieve the server information for the current instance of the db client
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
Retrieve the buildInfo for the current MongoDB instance
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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) { assert.ok(result); // Retrive the build information for the MongoDB instance adminDb.buildInfo(function(err, info) { assert.ok(err == null); adminDb.removeUser('admin3', function(err, result) { assert.ok(result); db.close(); }); }); }); }); });
Retrieve this db’s server status.
Arguments: |
|
---|---|
Returns: | null |
Examples
Retrieve the current server Info
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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.insert({'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) { assert.equal(null, err); assert.ok(info != null); adminDb.removeUser('admin13', function(err, result) { assert.ok(result); db.close(); }); }); }); }); }); });
Retrieve the current profiling Level for MongoDB
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
Retrieve the current profiling level set for the MongoDB instance
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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.insert({'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) { assert.ok(result); db.close(); }); }); }); }); }); });
Ping the MongoDB server and retrieve results
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
An example of how to add a user to the admin database
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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) { assert.ok(result); // Ping the server adminDb.ping(function(err, pingResult) { assert.equal(null, err); adminDb.removeUser('admin9', function(err, result) { assert.ok(result); db.close(); }); }); }); }); });
Authenticate against MongoDB
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
Authenticate against MongoDB Admin user
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); db.open(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.insert({'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) { assert.ok(result); adminDb.removeUser('admin2', function(err, result) { assert.ok(result); db.close(); }); }); }); }); });
Logout current authenticated user
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
An example of how add a user, authenticate and logout
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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) { assert.ok(result); // Logout the user adminDb.logout(function(err, result) { assert.equal(true, result); adminDb.removeUser('admin10', function(err, result) { assert.ok(result); db.close(); }); }); }); }); });
Add a user to the MongoDB server, if the user exists it will overwrite the current password
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
An example of how to add a user to the admin database
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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) { assert.ok(result); adminDb.removeUser('admin11', function(err, result) { assert.ok(result); db.close(); }); }); }); });
Remove a user from the MongoDB server
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
An example of how to remove a user from the admin database
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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) { assert.ok(result); // Remove the user adminDb.removeUser('admin12', function(err, result) { assert.equal(null, err); assert.equal(true, result); // Authenticate using the removed user should fail adminDb.authenticate('admin12', 'admin12', function(err, result) { assert.ok(err != null); assert.ok(!result); db.close(); }); }) }); }); });
Set the current profiling level of MongoDB
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
An example of how to use the setProfilingInfo Use this command to set the Profiling level on the MongoDB server
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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.insert({'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) { assert.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) { assert.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) { assert.equal('all', level); // Attempt to set an illegal profiling level adminDb.setProfilingLevel('medium', function(err, level) { assert.ok(err instanceof Error); assert.equal("Error: illegal profiling level value medium", err.message); adminDb.removeUser('admin6', function(err, result) { assert.ok(result); db.close(); }); }); }) }); }) }); }) }); }); }); }); });
Retrive the current profiling information for MongoDB
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
An example of how to use the profilingInfo Use this command to pull back the profiling information currently set for Mongodb
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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.insert({'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) { assert.ok(infos.constructor == Array); assert.ok(infos.length >= 1); assert.ok(infos[0].ts.constructor == Date); assert.ok(infos[0].millis.constructor == Number); adminDb.removeUser('admin7', function(err, result) { assert.ok(result); db.close(); }); }); }); }); }); }); }); }); });
Execute a db command against the Admin database
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
Retrieve the buildInfo using the command function
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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) { assert.ok(result); // Retrive the build information using the admin command adminDb.command({buildInfo:1}, function(err, info) { assert.ok(err == null); adminDb.removeUser('admin4', function(err, result) { assert.ok(result); db.close(); }); }); }); }); });
Validate an existing collection
Arguments: |
|
---|---|
Returns: | null Returns no result |
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 Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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.insert({'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) { // Pre 1.9.1 servers if(doc.result != null) { assert.ok(doc.result != null); assert.ok(doc.result.match(/firstExtent/) != null); } else { assert.ok(doc.firstExtent != null); } adminDb.removeUser('admin8', function(err, result) { assert.ok(result); db.close(); }); }); }); }); }); });
List the available databases
Arguments: |
|
---|---|
Returns: | null Returns no result |
Examples
An example of listing all available databases.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Use the admin database for the operation var adminDb = db.admin(); // List all the available databases adminDb.listDatabases(function(err, dbs) { assert.equal(null, err); assert.ok(dbs.databases.length > 0); db.close(); }); });
Get ReplicaSet status
Arguments: |
|
---|---|
Returns: | null |
Examples
Retrieve the current replicaset status if the server is running as part of a replicaset
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(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.insert({'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) { assert.equal(null, err); assert.ok(result != null); // Authenticate using the newly added user adminDb.authenticate('admin14', 'admin14', function(err, result) { assert.equal(null, err); assert.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) { assert.equal(null, err); assert.ok(result); db.close(); }); }) }); }); }); });