标签:孙悟空 操作 插入 增删改 add cti mongodb arguments find
var mongooes=require("mongoose"); mongooes.connect("mongodb://localhost/my_test",{useMongoClient:true}) mongooes.connection.once("open",function(){ console.log("数据库连接成功"); }) var Schema=mongooes.Schema; var stuSchema=new Schema({ name:String, age:Number, address:String, gender:{ type:String, default:"female" } }); var StuModel=mongooes.model("student",stuSchema); //使用model进行增删改查 //向数据库插入文档 StuModel.create({ name:"孙悟空", age:18, gender:"male", address:"花果山" },function(err){ if(!err) { console.log("插入成功") } }) StuModel.create([ { name:"猪八戒", age:"22", gender:‘female‘ },{ name:"唐僧", age:"34", gender:"male" } ],function(error){ if(!error) { console.log("插入成功") console.log(arguments) } }) StuModel.find({name:"唐僧"},function(err,docs){ if(!err) { console.log(docs); } }); StuModel.find({},function(err,docs){ if(!err) { console.log(docs); } }); StuModel.update({name:"唐僧"},{$set:{address:"女儿国"}},function(error){ if(!error) { console.log("修改成功") } }) StuModel.remove({name:"孙悟空"},function(err){ if(!err) { console.log("删除成功") } })
标签:孙悟空 操作 插入 增删改 add cti mongodb arguments find
原文地址:https://www.cnblogs.com/1521681359qqcom/p/13279170.html