Created
January 9, 2024 11:22
-
-
Save sangwin/2a660b25053c56559df917af6ccd5be4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb'); | |
const uri = "mongodb+srv://<username>:<Password>@localhost?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 | |
await client.connect(); | |
// Create connection | |
await client.db("users").createCollection("userList"); | |
// Insert a record | |
await client.db("users").collection('userList').insertOne({ | |
name : 'Sangwin', | |
email : '[email protected]' | |
}); | |
// Find a record | |
var dbo = client.db("users"); | |
const datas = await dbo.collection("userList").findOne({ _id: new ObjectId("<ID>") }); | |
} finally { | |
// Ensures that the client will close when you finish/error | |
await client.close(); | |
} | |
} | |
run().catch(console.dir); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment