MERN

⌘K
  1. Home
  2. Docs
  3. MERN
  4. Limit()

Limit()

// Limit()

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 limit() {
  try {
    await connectToMongo();

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

    const cursor = await collection.find({}).limit(10); // limit the results to 10 documents

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

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

limit();

How can we help?