From d76a4c5956f5dd0fe0877fba975a844075844441 Mon Sep 17 00:00:00 2001 From: 637f075a0567bd2c57c77830 <637f075a0567bd2c57c77830@devide> Date: Sun, 25 Jun 2023 09:09:00 +0000 Subject: [PATCH] Sun Jun 25 09:09:00 UTC 2023 inscode --- index.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..78fe026 --- /dev/null +++ b/index.js @@ -0,0 +1,42 @@ + +const { MongoClient, ServerApiVersion } = require('mongodb'); +const uri = "mongodb+srv://wrvr:qqqq258369@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 { + // Connect the client to the server (optional starting in v4.7) + await client.connect(); + // Send a ping to confirm a successful connection + await client.db("admin").command({ ping: 1 }); + console.log("Pinged your deployment. You successfully 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: 'Alice3', 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 { + // Ensures that the client will close when you finish/error + await client.close(); + } +} +run().catch(console.dir); -- GitLab