Class: Admin

Admin

new Admin(){Admin}

Create a new Admin instance (INTERNAL TYPE, do not instantiate directly)

Returns:
collection instance.

Methods

addUser(username, password, options, callback){null}

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

The command result callback

Example
// 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();
        });
      })            
    });                
  });
});

authenticate(username, password, callback){null}

Authenticate a user against the server.

Name Type Description
username string

The username.

password string optional

The password.

callback Admin~resultCallback

The command result callback

Example
// 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.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) {
        test.ok(result);

        adminDb.removeUser('admin2', function(err, result) {
          test.ok(result);

          db.close();
        });
      });
    });
  });
});

buildInfo(callback){null}

Retrieve the server information for the current instance of the db client

Name Type Description
callback Admin~resultCallback

The command result callback

Example
// 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();
        });
      });
    });
  });
});            

command(command, options, callback){null}

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

The command result callback

Example
// 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();
        });
      });
    });
  });
});

listDatabases(callback){null}

List the available databases

Name Type Description
callback Admin~resultCallback

The command result callback.

Example
// 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();
  });
});

logout(callback){null}

Logout user from server, fire off on all connections and remove all auth info

Name Type Description
callback Admin~resultCallback

The command result callback

Example
// 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();
        });
      });
    });                
  });
});

ping(callback){null}

Ping the MongoDB server and retrieve results

Name Type Description
callback Admin~resultCallback

The command result callback

Example
// 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();
        });
      });
    });
  });
});

profilingInfo(callback){null}

Retrive the current profiling information for MongoDB

Name Type Description
callback Admin~resultCallback

The command result callback.

Example
// 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.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) {
                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();
                });
              });
            });
          });
        });
      });
    });
  });
});

profilingLevel(callback){null}

Retrieve the current profiling Level for MongoDB

Name Type Description
callback Admin~resultCallback

The command result callback

Example
// 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.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) {
            test.ok(result);

            db.close();
          });
        });
      });
    });
  });
});

removeUser(username, options, callback){null}

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

The command result callback

Example
// 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();
        });
      })            
    });                
  });
});

replSetGetStatus(callback){null}

Get ReplicaSet status

Name Type Description
callback Admin~resultCallback

The command result callback.

Example
// 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.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) {
      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();
          });
        })
      });
    });
  });
});

serverInfo(callback){null}

Retrieve the server information for the current instance of the db client

Name Type Description
callback Admin~resultCallback

The command result callback

serverStatus(callback){null}

Retrieve this db's server status.

Name Type Description
callback Admin~resultCallback

The command result callback

Example
// 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.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) {
          test.equal(null, err);
          test.ok(info != null);
         
          adminDb.removeUser('admin13', function(err, result) {
            test.ok(result);

            db.close();
          });
        });
      });
    });
  });
});

setProfilingLevel(level, callback){null}

Set the current profiling level of MongoDB

Name Type Description
level string

The new profiling level (off, slow_only, all).

callback Admin~resultCallback

The command result callback.

Example
// 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.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) {
            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();
                      });
                    });
                  })
                });
              })
            });
          })
        });
      });
    });
  });
});

validateCollection(collectionName, options, callback){null}

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

The command result callback.

Example
// 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.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) {
            test.ok(doc.result != null);
            test.ok(doc.result.match(/firstExtent/) != null);                    
          } else {
            test.ok(doc.firstExtent != null);
          }

          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();
        });
      });
    });
  });
});

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.

comments powered by Disqus
Documentation generated by JSDoc 3.3.0-alpha9 on Wed Oct 29 2014 13:10:23 GMT+0100 (CET)