2. Session
[malex@thermalite mongodb-atlas-client]$ ls -l
total 0
[malex@thermalite mongodb-atlas-client]$ npm init -y
Wrote to /home/malex/nodejs/mongodb-atlas-client/package.json:
{
"name": "mongodb-atlas-client",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
[malex@thermalite mongodb-atlas-client]$ npm i mongodb -s
+ mongodb@3.5.9
added 20 packages from 11 contributors and audited 20 packages in 1.204s
1 package is looking for funding
run `npm fund` for details
found 0 vulnerabilities
[malex@thermalite mongodb-atlas-client]$ vi connection.js
[malex@thermalite mongodb-atlas-client]$ cat connection.js
const { MongoClient } = require('mongodb');
async function main() {
/**
* Connection URI. Update
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
const uri = "mongodb+srv://node_user:pass123@cluster101.n7nqjv.mongodb.net/test?retryWrites=true&w=majority";
/**
* The Mongo Client you will use to interact with your database
* See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details
*/
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
// Make the appropriate DB calls
await listDatabases(client);
} catch (e) {
console.error(e);
} finally {
// Close the connection to the MongoDB cluster
await client.close();
}
}
main().catch(console.error);
/**
* Print the names of all available databases
* @param {MongoClient} client A MongoClient that is connected to a cluster
*/
async function listDatabases(client) {
databasesList = await client.db().admin().listDatabases();
console.log("Databases:");
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
};
[malex@thermalite mongodb-atlas-client]$ node connection
(node:351200) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Databases:
- sample_airbnb
- sample_analytics
- sample_geospatial
- sample_mflix
- sample_restaurants
- sample_supplies
- sample_training
- sample_weatherdata
- admin
- local
[malex@thermalite mongodb-atlas-client]$
No comments:
Post a Comment