To read data from database using mongo driver

 const MongoClient=require("mongodb").MongoClient;


// Replace the uri string with your MongoDB deployment's connection string.
const url = "mongodb://localhost:27017";

const client = new MongoClient(url);

async function run() {
  try {
    await client.connect();
   
    const cursor = client.db("newFruits").collection("fruits").find({});

    const results = await cursor.toArray();

    if (results.length > 0) {
        console.log(`Found listing(s)`);
        results.forEach((result, i) => {
           
            console.log(`$_id: ${result._id} name: ${result.name} review:${result.rating} `);
           
               });
    } else {
        console.log(`No listings found!`);
    }
    // const result = await foods.insertMany(docs, options);
    // console.log(`${result.insertedCount} documents were inserted`);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);


Comments

Popular posts from this blog

To do list (without styling and css)

Template, layout and scope