①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