1-1 安装egg-mongodb

  1. cnpm i egg-mongodb -S

1-2 config/plugin.js

image.png

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

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

image.png

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

1-4 查询数据库

image.png

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

image.png