一、向mongodb数据库添加一条数据

insertOne

  1. /models/index.js
  2. const { MongoClient} = require('mongodb');
  3. // Connection URL
  4. const url = 'mongodb://124.71.174.116:10040';
  5. const client = new MongoClient(url);
  6. const dbName = 'Student';
  7. async function main() {
  8. await client.connect();
  9. console.log('Connected successfully to server');
  10. const db = client.db(dbName);
  11. const collection = db.collection('member');
  12. return collection;
  13. }
  14. module.exports = main;
router.get("/add",async ctx=>{
    var  collection = await main();
    var obj = {
        name:"吴章",
        age:17
    }
    var res = await collection.insertOne(obj);
    console.log(res)
})