1、安装

  1. cnpm i egg-mongodb -S

2、配置config/pligin.js

  1. module.exports = {
  2. ...
  3. mongodb:{
  4. enable:true,
  5. package:"egg-mongodb"
  6. }
  7. };

3、配置config/config.default.js

  1. config.mongodb = {
  2. app: true,
  3. agent: false,
  4. client:{
  5. hosts: '121.36.217.123:12021',
  6. db: 'user',
  7. }
  8. }

4、链接mongodb

  1. class MusicController extends Controller {
  2. async index() {
  3. const { ctx } = this;
  4. var db = this.app.mongodb;
  5. var res =await db.collection("user").find().toArray();
  6. console.log(res)
  7. }
  8. }