1-1 安装

  1. cnpm i egg-mongodb -S

1-2 config/plugin.js

image.png

module.exports = {
  ...
  mongodb:{
    enable:true,
    package:"egg-mongodb"
  }
};

1-3 配置config/config.default.js

config.mongodb = {
    app: true,
    agent: false,
    client:{
      hosts:"123.60.82.25:12021",
      db:"student"
    }
  }

1-4 查询数据库

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)
  }
}