MERN

⌘K
  1. Home
  2. Docs
  3. MERN
  4. Find() ডাটাবেস all থেকে ডাটা পড়া

Find() ডাটাবেস all থেকে ডাটা পড়া

// Find()
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 find() {
  try {
    await connectToMongo();

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

    const query = {}; // Query will be here

    const cursor = await collection.find(query);

    while (await cursor.hasNext()) {
      const document = await cursor.next();
      console.log("Document found:");
      console.log(document);
    }

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

find();

How can we help?