时间:2021-07-01 10:21:17 帮助过:6人阅读
modle/order.js
module.exports = app => { const mongoose = app.mongoose; /*引入建立连接的mongoose */ const Schema = mongoose.Schema; var OrderSchema=Schema({ order_id:String, uid:Number, trade_no:String, all_price:Number, all_num:Number }) return mongoose.model(‘Order‘,OrderSchema,‘order‘); }
controller/order.js
‘use strict‘; const Controller = require(‘egg‘).Controller; class OrderController extends Controller { async index() { //实现关联查询 // var orderResult=await this.ctx.model.Order.find({}); var orderResult=await this.ctx.model.Order.aggregate([ { $lookup:{ from:‘order_item‘, localField:‘order_id‘, foreignField:‘order_id‘, as:‘items‘ } }, { $match:{"all_price":{$gte:90}} } ]); this.ctx.body=orderResult; } } module.exports = OrderController;
Egg中使用Mongoose实现数据库表的关联查询
标签:wait module lookup 使用 username mongo agg extend sync