MERN

⌘K
  1. Home
  2. Docs
  3. MERN
  4. ডাটা ইন্সার্ট করা IinsertMany()

ডাটা ইন্সার্ট করা IinsertMany()

// InsertMany()
const { MongoClient } = require("mongodb");
const client = new MongoClient("mongodb://localhost:27017");

async function connectToMongo() {
  try {
    await client.connect();
    console.log("Connected to MongoDB");
  } catch (err) {
    console.error(err);
  }
}

async function insertMany() {
  try {
    await connectToMongo();

    const db = client.db("test");
    const collection = db.collection("student");

    const data = [
      { name: "olee", roll: 23 },
      { name: "john", roll: 24 },
      { name: "doe", roll: 25 },
    ];

    const result = await collection.insertMany(data);
    console.log("Inserted documents with _ids: " + result.insertedIds);

    client.close();
  } catch (err) {
    console.error(err);
  }
}

insertMany();

How can we help?