连接到数据库
要从节点应用程序连接到 mongo 数据库,我们需要 mongoose。
安装 Mongoose 转到应用程序的 toot 并安装 mongoose
npm install mongoose
接下来我们连接到数据库。
var mongoose = require('mongoose');
//connect to the test database running on default mongod port of localhost
mongoose.connect('mongodb://localhost/test');
//Connecting with custom credentials
mongoose.connect('mongodb://USER:PASSWORD@HOST:PORT/DATABASE');
//Using Pool Size to define the number of connections opening
//Also you can use a call back function for error handling
mongoose.connect('mongodb://localhost:27017/consumers',
{server: { poolSize: 50 }},
function(err) {
if(err) {
console.log('error in this')
console.log(err);
// Do whatever to handle the error
} else {
console.log('Connected to the database');
}
});