egg mongoose 学习记录

mongoose 安装

npm i egg-mongoose -S
config/plugin.js
mogoose: {
  enable: true,
  package: 'egg-mongoose',
}

config/config.default.js
config.mongoose = {
  url: 'mongodb://localhost:27017/runoob',
  option: {
    server: {
      poolSize: 40,
    },
  },

router

// app/router
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
  router.get('/add', controller.home.add);
  router.get('/find', controller.home.find);
  router.get('/insert', controller.home.insert);
  router.get('/deleteOne', controller.home.delete);
  router.get('/deleteMany', controller.home.deleteMany);
};

controller

'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async add() {
    const result = await this.ctx.service.user.add();
    this.ctx.body = result;
  }
  async find() {
    const query = this.ctx.query;
    console.log('query:', query);
    const result = await this.ctx.service.user.find(query);
    this.ctx.body = result;
  }
  async insert() {
    const query = this.ctx.query;
    const result = await this.ctx.service.user.insert(query);
    this.ctx.body = result;
  }
  async delete() {
    const query = this.ctx.query;
    const result = await this.ctx.service.user.deleteOne(query);
    return result;
  }
  async deleteMany() {
    const query = this.ctx.query;
    const result = await this.ctx.service.user.deleteMany(query);
    return result;
  }
}

module.exports = HomeController;

service

'use strict';
const Service = require('egg').Service;
class UserService extends Service {
  // 更新用户信息
  async add() {
    const result = await this.ctx.model.User.create({
      name: 'xiaoMinghui',
      age: '20',
      sex: '1',
    });
    return result;
  }

  // 查询数据
  // .sort({_id: -1}) 按照创建时间倒序
  // .skip(pageSize * (currectPage - 1))  跳过前n个数据
  // .limit(pageSize) 限制n个数据
  async find(query) {
    const result = await this.ctx.model.User.find(query);
    return result;
  }

  // 增加数据
  async insert(query) {
    const result = await this.ctx.model.User.create(query);
    return result;
  }
  async deleteOne(query) {
    const result = await this.ctx.model.User.deleteOne(query);
    return result;
  }
  async deleteMany(query) {
    const result = await this.ctx.model.User.remove(query);
    return result;
  }
}
module.exports = UserService;

model 定义

// model/User.js
'use strict';
module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const UserSchema = new Schema({
    name: {
      type: String,
    },
    age: {
      type: String,
    },
    sex: {
      type: String,
    },
  });
  return mongoose.model('User', UserSchema, 'test');
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值