connect to mongoDB cluster code example

Example: connect to mongoDB cluster

const { MongoClient } = require("mongodb");// Replace the uri string with your MongoDB deployment's connection string.const uri =  "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&writeConcern=majority";const client = new MongoClient(uri, {  useNewUrlParser: true,  useUnifiedTopology: true,});async function run() {  try {    await client.connect();    const database = client.db('sample_mflix');    const movies = database.collection('movies');    // Query for a movie that has the title 'Back to the Future'    const query = { title: 'Back to the Future' };    const movie = await movies.findOne(query);    console.log(movie);  } finally {    // Ensures that the client will close when you finish/error    await client.close();  }}run().catch(console.dir);

Tags:

Misc Example