请确定您已阅读mongoose教程–工程搭建
GitHub源码链接:sodino#MongoDemo
connection的事件列表可点击查看官方文档
或./node_modules/mongoose/lib/connection.js#Connection()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| // 导入mongoose库 var mongoose = require('mongoose'); // 获得db对象 db = mongoose.connection; // 各种事件 // connection的事件列表可查看:http://mongoosejs.com/docs/api.html#connection_Connection // 或 ./node_modules/mongoose/lib/connection.js#Connection() db.on('error', console.error.bind(console, 'connection error:')); db.on('open', ()=>{ console.log('db open'); }); db.on('connecting', ()=>{ console.log('db connecting...'); }); db.on('connected', ()=>{ console.log('db connected'); }); db.on('disconnecting', ()=>{ console.log('db disconnecting...'); }); db.on('disconnected', ()=>{ console.log('db disconnected'); }); db.on('close', ()=>{ console.log('db close'); }); // 启动db链接 mongoose.connect('mongodb://localhost:27017/Phone'); // 关闭的两种方式 // mongoose.connection.close(); 等同于 db.close(); mongoose.disconnect();
|
启动db链接中端口号27017
来自mongod运行后所打印出的日志中获得。详情见mongoose教程–工程搭建
下一篇:mongoose教程–Schema与Model
About Sodino