const { MongoClient, ServerApiVersion } = require('mongodb'); const uri = 'mongodb+srv://:@cluster0.kmggvco.mongodb.net/?retryWrites=true&w=majority'; // Create a MongoClient with a MongoClientOptions object to set the Stable API version const client = new MongoClient(uri, { serverApi: { version: ServerApiVersion.v1, strict: true, deprecationErrors: true, } }); async function run() { try { await client.connect(); console.log('Connected to MongoDB!'); // Get a reference to the database const db = client.db('mydatabase'); // Get a reference to the collection const collection = db.collection('mycollection'); // Insert a document const resultInsert = await collection.insertOne({ name: 'Alice', age: 30 }); console.log(`Inserted ${resultInsert.insertedCount} documents into the collection`); // Find documents const resultFind = await collection.find({ age: { $gt: 25 } }).toArray(); console.log(`Found ${resultFind.length} documents that match the query`); console.log(resultFind); } finally { await client.close(); console.log('Disconnected from MongoDB!'); } } run().catch(console.error);