1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
=================
Database Commands
=================
Database commands allow you to perform a wide range of diagnostic and administrative
tasks with the Node.js driver. For example, the
:manual:`dbStats </reference/command/dbStats/>` command returns
storage statistics for a given database. Use the ``command`` function to access
database commands.
.. code-block:: js
const { MongoClient } = require('mongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Create a new MongoClient
const client = new MongoClient(url);
async function main(client) {
const db = client.db('myproject');
const results = await db.command({ dbStats: 1 });
console.log(results);
}
// Function to connect to the server and run your code
async function run() {
try {
// Connect the client to the server
await client.connect();
console.log('Connected successfully to server');
await main(client);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
// Runs your code
run();
For a complete list of database commands, see the :manual:`manual </reference/command/>` .
|