1、安装
cnpm i egg-mongodb -S
2、配置config/pligin.js
module.exports = {
...
mongodb:{
enable:true,
package:"egg-mongodb"
}
};
3、配置config/config.default.js
config.mongodb = {
app: true,
agent: false,
client:{
hosts: '121.36.217.123:12021',
db: 'user',
}
}
4、链接mongodb
class MusicController extends Controller {
async index() {
const { ctx } = this;
var db = this.app.mongodb;
var res =await db.collection("user").find().toArray();
console.log(res)
}
}