const { MongoClient } = require("mongodb");
async function run() {
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);
try {
await client.connect();
const database = client.db("sample_database");
const collection = database.collection("sample_collection");
// Insert sample data
const sampleData = [
{ name: "GFG1", age: 25 },
{ name: "GFG2", age: 30 },
{ name: "GFG3", age: 35 },
{ name: "GFG4", age: 40 },
{ name: "GFG5", age: 45 },
{ name: "GFG6", age: 50 },
{ name: "GFG7", age: 55 },
{ name: "GFG8", age: 60 },
{ name: "GFG9", age: 65 },
{ name: "GFG10", age: 70 },
];
// Ensure the collection is empty before inserting
await collection.deleteMany({});
await collection.insertMany(sampleData);
// Define the query and options
// Define your query here, {} means select all documents
const query = {};
const options = {
skip: 2, // Skip the first 2 documents
limit: 5, // Limit the result to 5 documents
};
const cursor = collection.find(query, options);
const results = await cursor.toArray();
console.log("Results:", results);
} finally {
await client.close();
}
}
run().catch(console.dir);