writing data to a 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 dbName="newFruits"
    const database = client.db(dbName);
    const foods = database.collection("fruits");

    // create an array of documents to insert
    const docs = [
      { name: "watermelon", healthy: true,rating:5 },
      { name: "mushroom ", healthy: true,rating:2 },
      { name: "mango", healthy: true,rating:4 }
    ];

    // this option prevents additional documents from being inserted if one fails
    const options = { ordered: true };
   
    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 read data from database using mongo driver

To do list (without styling and css)

Template, layout and scope