0

I'm using NodeJS with MongoDB using mongodb package. When I run mongod command it works fine and gives "Waiting for connection" But when I run app.js command it gives this

enter image description here

I have install mongo db version 6.0 and my code is-

const { MongoClient } = require("mongodb");

// Replace the uri string with your connection string.
const uri =
"mongodb://localhost:27017";

const client = new MongoClient(uri);

async function run() {
  try {
    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);

I have done everthing that it takes to work but i don't know why it's not working.

0 Answers0