一:参考学习网址
npm: https://www.npmjs.com/package/mongoose
官网API:
二:在node中的连接1.先要安装mongoose: npm i mongoose --save
2.如何连接:
var mongoose = require('mongoose');
//连接
mongoose.connect( ' 地址 ' ) //自己需要连接的数据库地址,如本机的test数据库,就是 mongoose.connect('mongodb://localhost/test')
var db = mongoose.connection;
//连接成功 db.on('connected', function () { console.log('Mongoose connection open to ' + DB_URL); });//连接异常 db.on('error',function (err) { console.log('Mongoose connection error: ' + err); });
//连接断开
db.on('disconnected', function () { console.log('Mongoose connection disconnected'); });
module.exports = mongoose;