egg学习笔记第二十二天:eggjs中使用使用egg-mongo-native实现表关联

①cnpm i egg-mongo-native --save  安装egg-mongo-native javascript

②config>plugin.js下配置插件java

exports.mongo = {
  enable: true,
  package: 'egg-mongo-native',
};

③config>config.default.js下配置数据库链接插件数据库

// 配置数据库
  exports.mongo = {
    client: {
      host: '127.0.0.1',
      port: '27017',
      name: 'eggcms',
      user: 'eggadmin',
      password: '123456',
      options: {

      },
    },
  };

④随便定义一个路由npm

    

⑤controller中写入:app

const result = await this.app.mongo.aggregate('order', {
      pipeline: [
        {
          $lookup: {
            from: 'order_item',
            localField: 'order_id',
            foreignField: 'order_id',
            as: 'items',
          },
        },
        {
          $match: { all_price: { $gte: 90 } },
        },
      ],
    });
    console.log(result);
    this.ctx.body = result;

order表与order_item表以order_id作关联,写入items字段中,在根据all_price字段大于90作过滤。this

这就是两表的关联查询spa

相关文章
相关标签/搜索