1 const mongoose=require('mongoose') 2 3 mongoose.connect("mongodb://localhost/playground",{ useNewUrlParser: true,useUnifiedTopology: true}) 4 .then(()=>{console.log("数据库连接成功")}) 5 .catch(err=>{console.log(err,'数据库失败')}) 6 7 const courseSchema= new mongoose.Schema({ 8 name:String, 9 author:String, 10 isPublished:Boolean 11 }); 12 //使用规则创建集合,参数1是集合名,2是规则 13 const Course= mongoose.model("Course",courseSchema) 14 //更新一个,前为条件,后为值,若要更新一大堆数据,只更新第一个 15 Course.updateOne({name:'javascript'},{name:'java'}).then(res=>{console.log(res)}) 16 //更新多个 17 Course.updateMany({},{isPublished:true}).then(res=>{console.log(res)})