// Sort()
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 sort() {
try {
await connectToMongo();
const db = client.db("test");
const collection = db.collection("student");
const cursor = await collection.find({}).sort({ name: 1 }); // sort by name in ascending order
while (await cursor.hasNext()) {
const document = await cursor.next();
console.log("Document:");
console.log(document);
}
client.close();
} catch (err) {
console.error(err);
}
}
sort();