美文网首页
egg-mongoose 增删改查

egg-mongoose 增删改查

作者: GaoEnron | 来源:发表于2019-12-05 15:48 被阅读0次

egg 基本框架的搭建

egg.js官网

一、egg.js 添加 egg-mongoose 插件

1. 在plugin.js中开启插件

/** @type Egg.EggPlugin */
module.exports = {
  // 安装的数据库操作
  mongoose: {
    enable: true,
    package: 'egg-mongoose',
  },
};

2. 在config.default.js 中添加mongoose相应的配置操作

// 配置相应的数据库()
  config.mongoose = {
    client: {
        // 'mongodb://127.0.0.1/eggxiaomi',
      url: 'mongodb://localhost:27017/statistics',
        options: {
            useUnifiedTopology: true,
        }
    },
  };

二、egg-mongoose具体的使用

1. 需要在app目录下建立 model 目录

目录结构
2. 在model目录下面建立对应的存储类
-  required: ```true```, 表示这个值是否是唯一
- type: ```String``` 表示数据的类型
type 包含的数据类型
数据类型 含义
Number 数字
String 数字
Boolean 布尔值
ObjectId 对象ID
Array 数组
Object 对象
Date 日期

创建相应 Event 对应存储实体Model

mongoose.model('Event', schema);  
module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const schema = new Schema({
    deviceId: {
      type: String,
      required: true,
    },

    statisticTime: {
      type: Date,
      required: false,
    },
    statisticType: {
      type: String,
      required: true,
    },
    // 语言类型
    page_name: {
      type: String,
      required: false,
    },
    // 页面的ID
    page_id: {
      type: String,
      required: true,
    },
    // Mac 地址
    page_start_time: {
      type: Date,
      required: false,
    },
    // 设备所处的平台
    page_end_time: {
      type: Date,
      require: false,
    },
    // refere_page_id
    referer_page_id: {
      type: String,
      require: false,
    },
  });
  //
  return mongoose.model('Event', schema);
};

3. 插入数据操作

  1. 存储单条数据
const result = ctx.model.Infor.create({ deviceDensity, deviceId, deviceLocale, deviceMacAddr,
        deviceModel, systemOsVersion, devicePlatform, appChannel , appVersion, ipAddress, wifi});
  1. 存储多条数据
const dataArray = [];
dataArray.push({ deviceDensity, deviceId, deviceLocale, deviceMacAddr,
        deviceModel, systemOsVersion, devicePlatform, appChannel , appVersion, ipAddress, wifi})
const result = ctx.model.Infor.create(dataArray);

相关文章

  • egg-mongoose 增删改查

    egg 基本框架的搭建 egg.js官网 一、egg.js 添加 egg-mongoose 插件 1. 在plug...

  • mysql的插入语句

    MySQL增删改查之增insert、replace

  • MYSQL数据库的增删改查

    MYSQL数据库的增删改查 一.对于库的增删改查 增create database 库名称;create data...

  • 关于python的list的增查删改

    说到增查删改,想起了数据库,我们在关系型数据库当中就会对表进行增查删改。 在python当中我们也可以对list进...

  • 0812 A

    mongodb 增删改查 增: db.createCollection("name", {options:numb...

  • 增删改

    对于表中的操作,就是增删改查,查内容较多,这里先说增删改。 1.增(insert或者load) 即插入数据,多行插...

  • SQL查询结构总结

    SQL 增删改查 对数据库有修改的操作是:增删改 增 insert into 表名 values(); 删 del...

  • 2018-03-03

    rails c增删改查 增:user = User.create(name:'Jack', age:'18') 删...

  • 函数和增删改查(运用)

    增删改查 (基本命令) 1 . 增 inset(position,value)append(value)exten...

  • 表内容的操作

    对表数据进行增删改查(curd) 增(insert into): auto_increment自增长 SELECT...

网友评论

      本文标题:egg-mongoose 增删改查

      本文链接:https://www.haomeiwen.com/subject/rmmdgctx.html