Sun Jun 25 09:09:00 UTC 2023 inscode

上级 2e987305
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);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册