Mon Jun 26 02:43:00 UTC 2023 inscode

上级 d76a4c59
var axios = require('axios');
const { MongoClient, ServerApiVersion } = require('mongodb'); var data = JSON.stringify({
const uri = "mongodb+srv://wrvr:qqqq258369@cluster0.kmggvco.mongodb.net/?retryWrites=true&w=majority"; "collection": "mycollection",
"database": "mydatabase",
// Create a MongoClient with a MongoClientOptions object to set the Stable API version "dataSource": "Cluster0",
const client = new MongoClient(uri, { "projection": {
serverApi: { "_id": 1
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
} }
}); });
async function run() { var config = {
try { method: 'post',
// Connect the client to the server (optional starting in v4.7) url: 'https://ap-southeast-1.aws.data.mongodb-api.com/app/data-jmriv/endpoint/data/v1/action/findOne',
await client.connect(); headers: {
// Send a ping to confirm a successful connection 'Content-Type': 'application/json',
await client.db("admin").command({ ping: 1 }); 'Access-Control-Request-Headers': '*',
console.log("Pinged your deployment. You successfully connected to MongoDB!"); 'api-key': 'RddbxMZlXN9uX3qKx39q58CoRWjkiyb8qihoOo5bLAnVVpmi5hmKeMHKQVvNfb8O',
},
// Get a reference to the database data: data
const db = client.db('mydatabase'); };
// Get a reference to the collection axios(config)
const collection = db.collection('mycollection'); .then(function (response) {
console.log(JSON.stringify(response.data));
// Insert a document })
const resultInsert = await collection.insertOne({ name: 'Alice3', age: 30 }); .catch(function (error) {
console.log(`Inserted ${resultInsert.insertedCount} documents into the collection`); console.log(error);
});
// 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.
先完成此消息的编辑!
想要评论请 注册