1. const mongoose = require("mongoose");导入mongoose模块 2. //注意url地址最后面的地址是数据库的名称 const url = "mongodb://127.0.0.1:27017/bk1824"; 3. mongoose.connect(url,(err)=>{ if(err){ console.log("链接失败") }else{ console.log("链接成功") } }) 4. //存 const User = mongoose.model("user",{ username:String, password:String }) 5. //增数据 const user = new User({ username:"zhao1234", password:"12345" }) 6. //将增加的数据存入数据库的表中 user.save().then((res)=>{ console.log(res) }) 7. // 删除数据 User.remove({username:"zhao1234"}).then((res)=>{ console.log(res) }) 8. //改数据 User.update({username:"1111"},{$set:{username:"zhao"}}).then((res)=>{ console.log(res); }) 9.. // 查数据 User.find({username:"zhao"}).then((res)=>{ console.log(res); })