MongoDb and Node.js: Listing server databases
Certain
applications needs to identify the existing database instances in a certain database
server. For this purpose, the database libraries provides mechanisms to identify
metadata over its database instances, as its name, size on disk and amount of
stored records/documents.
For identifying
the existing databases in a mongodb server connection from node.js programming
language you may run a code like this example below:
//npm install mongodb
const {MongoClient} = require('mongodb');
const uri = "mongodb://localhost:27017/";//$ mongo mongodb://<host>:<port>
const client = new MongoClient(uri);
client.connect().then(function(){
//client.db().
client.db().admin().listDatabases().then(function(databases){
console.log("Databases:",databases);
});
Just to not
forget that before running the code it is necessary to install though npm the
mongodb module.
Also it is
important to highlight that here in the example the URI is pointing to a local
database in its default port, but in another scenarios you may need to adjust
that.
The
complete code of this example is available here:
https://github.com/rafaelqg/code/blob/main/node_mongo_list_databases_async.js
A video
class about this code you can see here:
Comments
Post a Comment