github文档
1.mongodb插入一条数据
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
if(err) throw err;
client.db('jike').collection('user').insertOne({name:"chengchao"},(err,res)=>{
if(err) throw err;
console.log('one document inserted');
client.close()
})
})
2.修改updateOne
MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
if(err) throw err;
client.db('jike').collection('user').updateOne({"name":"jiangwei"},{
$set:{"name":"李四"}
},(err,res)=>{
if(err) throw err;
})
})
3.删除deleteOne
MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
if(err) throw err;
client.db('jike').collection('user').deleteOne({name:'jiangwei'},(err,res)=>{
if(err) throw err;
console.log('one document delete');
client.close()
})
})
4.查询find
MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{
if(err) throw err;
client.db('jike').collection('user').find().toArray((err,res)=>{
if(err) throw err;
console.log(res);
client.close()
})
})
结合koa实现增删改查