mongoose的连接
const mongoose=require('mongoose');
mongoose.connect("mongodb://localhost:27017/study", {useNewUrlParser:true});//不加配置对象会有警告 const db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:连接失败'));//on和once似乎区别不大 db.once('open', function() { console.log("we're connected!"); //在此函数中执行各项命令 })
mongoose选定已有数据表
注意是在Schema实例中配置
const Schema=mongoose.Schema; const blogSchema=new Schema({ name:String, age:Number, sex:String, time:String },{versionKey: false,collection:'user'}); //versionKey:false使数据不自动添加_V:""; //collection:'user'使数据存入user的数据表中,而不再新建! const blog=mongoose.model('blog',blogSchema);