1. 查询每个order订单中的所有orderItem(多表关联查询)
order.js:
var mongoose = require('./db.js')var OrderSchema = mongoose.Schema({order_id: String,uid: Number,trade_no: String,all_price: Number,all_num: Number
})var OrderModel = mongoose.model('Order',OrderSchema, 'order')module.exports = OrderModel
order_item.js:
var mongoose = require('./db.js')var OrderItemSchema = mongoose.Schema({order_id: String,title: String,price: Number,num: Number
})var OrderItemModel = mongoose.model('OrderItem',OrderItemSchema, 'order_item')module.exports = OrderItemModel
app.js:
var OrderModel = require('./model/order.js')// var OrderItemModel = require('./model/order_item.js')// order表 关联 order_item表// 查询order表的数据
// OrderModel.find({}, function (err,data) {
// if (err) {
// console.log(err)
// return;
// }
// console.log(data)
// })OrderModel.aggregate([{$lookup: {from: "order_item",localField: "order_id",foreignField: "order_id",as: "items"}}
], function (err, data) {if (err) {console.log(err)return;}console.log(JSON.stringify(data))
})
多表查询结果:
// 1
{"_id": ObjectId("606ecfbdbb390000fa004964"),"order_id": "1","uid": 10,"trade_no": "111","all_price": 100,"all_num": 2,"items": [{"_id": ObjectId("606ecfbdbb390000fa004967"),"order_id": "1","title": "商品鼠标 1","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004968"),"order_id": "1","title": "商品键盘 2","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004969"),"order_id": "1","title": "商品键盘 3","price": 0,"num": 1}]
}// 2
{"_id": ObjectId("606ecfbdbb390000fa004965"),"order_id": "2","uid": 7,"trade_no": "222","all_price": 90,"all_num": 2,"items": [{"_id": ObjectId("606ecfbdbb390000fa00496a"),"order_id": "2","title": "牛奶","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa00496b"),"order_id": "2","title": "酸奶","price": 40,"num": 1}]
}// 3
{"_id": ObjectId("606ecfbdbb390000fa004966"),"order_id": "3","uid": 9,"trade_no": "333","all_price": 20,"all_num": 6,"items": [{"_id": ObjectId("606ecfbdbb390000fa00496c"),"order_id": "3","title": "矿泉水","price": 2,"num": 5},{"_id": ObjectId("606ecfbdbb390000fa00496d"),"order_id": "3","title": "毛巾","price": 10,"num": 1}]
}
2. 查询每个order订单中的所有orderItem(多表关联查询),并且筛选出每个order订单的总价all_price>=90的订单
app.js:
var OrderModel = require('./model/order.js')// var OrderItemModel = require('./model/order_item.js')// order表 关联 order_item表// 查询order表的数据
// OrderModel.find({}, function (err,data) {
// if (err) {
// console.log(err)
// return;
// }
// console.log(data)
// })OrderModel.aggregate([{$lookup: {from: "order_item",localField: "order_id",foreignField: "order_id",as: "items"}},{$match: {"all_price": { $gte: 90 }}}
], function (err, data) {if (err) {console.log(err)return;}console.log(JSON.stringify(data))
})
// 1
{"_id": ObjectId("606ecfbdbb390000fa004964"),"order_id": "1","uid": 10,"trade_no": "111","all_price": 100,"all_num": 2,"items": [{"_id": ObjectId("606ecfbdbb390000fa004967"),"order_id": "1","title": "商品鼠标 1","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004968"),"order_id": "1","title": "商品键盘 2","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004969"),"order_id": "1","title": "商品键盘 3","price": 0,"num": 1}]
}// 2
{"_id": ObjectId("606ecfbdbb390000fa004965"),"order_id": "2","uid": 7,"trade_no": "222","all_price": 90,"all_num": 2,"items": [{"_id": ObjectId("606ecfbdbb390000fa00496a"),"order_id": "2","title": "牛奶","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa00496b"),"order_id": "2","title": "酸奶","price": 40,"num": 1}]
}
3. 查询order_item 找出商品名称是酸奶的商品对应的订单的订单号以及订单的总价格
第一种实现方式:采用两次查询