文档

github文档

1.mongodb插入一条数据

  1. const MongoClient = require('mongodb').MongoClient;
  2. const url = 'mongodb://localhost:27017';
  3. MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
  4. if(err) throw err;
  5. client.db('jike').collection('user').insertOne({name:"chengchao"},(err,res)=>{
  6. if(err) throw err;
  7. console.log('one document inserted');
  8. client.close()
  9. })
  10. })

2.修改updateOne

  1. MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
  2. if(err) throw err;
  3. client.db('jike').collection('user').updateOne({"name":"jiangwei"},{
  4. $set:{"name":"李四"}
  5. },(err,res)=>{
  6. if(err) throw err;
  7. })
  8. })

3.删除deleteOne

  1. MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
  2. if(err) throw err;
  3. client.db('jike').collection('user').deleteOne({name:'jiangwei'},(err,res)=>{
  4. if(err) throw err;
  5. console.log('one document delete');
  6. client.close()
  7. })
  8. })

4.查询find

  1. MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
  2. if(err) throw err;
  3. client.db('jike').collection('user').find().toArray((err,res)=>{
  4. if(err) throw err;
  5. console.log(res);
  6. client.close()
  7. })
  8. })

结合koa实现增删改查