安装

  1. cnpm i egg-mongodb -S

配置config/config.default.js

  1. config.mongodb = {
  2. pp: true,
  3. agent: false,
  4. client: {
  5. hosts: '123.60.37.66:10040',
  6. db: 'Student',
  7. }
  8. }

配置config/plugin.js

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

查询数据库

colltroller/..js

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