1-1 安装egg-mongodb

  1. cnpm i egg-mongodb -S

1-2 config/plugin.js

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

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

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

1-4 查询数据库

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