Database Commands

Database commands allow you to perform a wide range of diagnostic and administrative tasks with the Node.js driver. For example, the dbStats command returns storage statistics for a given database. Use the command function to access database commands.

// set up a command function
function getDbStats(db, callback) {
  db.command({'dbStats': 1}, function(err, results) {
    console.log(results);
    callback();
  });
};

// use the function
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  const db = client.db(dbName);
  getDbStats(db, function() {
    client.close();
  });
});

For a complete list of database commands, see the manual.

On this page