db.order.insert({"order_id":"1","uid":10,"trade_no":"111","all_price":100,"all_num":2}) db.order.insert({"order_id":"2","uid":7,"trade_no":"222","all_price":90,"all_num":2}) db.order.insert({"order_id":"3","uid":9,"trade_no":"333","all_price":20,"all_num":6}) db.order_item.insert({"order_id":"1","title":"商品鼠標 1","price":50,num:1}) db.order_item.insert({"order_id":"1","title":"商品鍵盤 2","price":50,num:1}) db.order_item.insert({"order_id":"1","title":"商品鍵盤 3","price":0,num:1}) db.order_item.insert({"order_id":"2","title":"牛奶","price":50,num:1}) db.order_item.insert({"order_id":"2","title":"酸奶","price":40,num:1}) db.order_item.insert({"order_id":"3","title":"礦泉水","price":2,num:5}) db.order_item.insert({"order_id":"3","title":"毛巾","price":10,num:1})
db.js
var mongoose = require('mongoose'); mongoose.connect('mongodb://eggadmin:123456@127.0.0.1:27017/eggcms',{ useNewUrlParser: true },function(err){ if(err){ console.log(err) return } console.log("數據庫鏈接成功") }) module.exports = mongoose;
order.js
var mongoose = require('./db.js'); var OrderSchema = mongoose.Schema({ order_id:{ type:String, }, uid:Number, trade_no:String, all_price:Number, all_num:Number, }) var OrderSchema = mongoose.model('Order',OrderSchema,'order'); module.exports = OrderSchema
order_item.js
var mongoose = require('./db.js'); var OrderItemSchema = mongoose.Schema({ order_id:{ type:String, }, title:String, price:Number, num:Number, }) var OrderItemSchema = mongoose.model('OrderItem',OrderItemSchema,'order_item'); module.exports = OrderItemSchema
app.js
var OrderModel = require('./order.js'); OrderModel.aggregate([{ $lookup:{ from:"order_item", localField:"order_id", foreignField:"order_id", as:"item" } }],function(err,docs){ if(err){ console.log(err); return; } console.log(JSON.stringify(docs)) })
能夠用來重命名,增長或者刪除文檔中的字段
要求查找 order 只返回文檔中 trade_no 和 all_price 字段mongodb
app.js
OrderModel.aggregate([ { $project:{ trade_no:1, all_price:1 } } ], function (err, docs) { if (err) { console.log(err); return; } console.log(docs) })
獲得總價格>90的數據數據庫
app.js
var OrderModel = require('./order.js'); var OrderItemModel = require('./order_item.js'); OrderModel.aggregate([{ $lookup: { from: "order_item", localField: "order_id", foreignField: "order_id", as: "item" }, }, { $match: { "all_price": { $gte: 90 } } }], function (err, docs) { if (err) { console.log(err); return; } // console.log(docs) console.log(JSON.stringify(docs)) })
將集合中的文檔進行分組,可用於統計結果。
統計每一個訂單的訂單數量,按照訂單號分組app
app.js
OrderItemModel.aggregate([{ $group:{ _id:"$order_id", total:{$sum:"$sum"} } }],function (err, docs) { if (err) { console.log(err); return; } // console.log(docs) console.log(JSON.stringify(docs)) })
1爲升序,-1爲降序mongoose
app.js
OrderModel.aggregate([ { $project:{ trade_no:1, all_price:1 } }, { $sort:{"all_price":1} } ], function (err, docs) { if (err) { console.log(err); return; } console.log(docs) })
只查一條ui
app.js
OrderModel.aggregate([ { $project:{ trade_no:1, all_price:1 } }, { $sort:{"all_price":1} }, { $limit:1 } ], function (err, docs) { if (err) { console.log(err); return; } console.log(docs) })
app.js
OrderModel.aggregate([ { $project:{ trade_no:1, all_price:1 } }, { $sort:{"all_price":1} }, { $skip:1 } ], function (err, docs) { if (err) { console.log(err); return; } console.log(docs) })
查詢order_item,找出商品名稱是酸奶的商品,酸奶這個商品對應的訂單的訂單號和訂單總價格等信息
app.js
var OrderModel = require('./order.js'); var OrderItemModel = require('./order_item.js'); OrderItemModel.find({ "_id": "5bffa9bbd8cb3dff1d3d6658" }, function (err, docs) { var order_item = JSON.parse(JSON.stringify(docs)); var order_id = order_item[0].order_id; OrderModel.find({ "order_id": order_id }, function (err, order) { order_item.order_info = order[0]; console.log(order_item) }) })
app.js
var OrderItemModel = require('./order_item.js'); var mongoose = require("mongoose") OrderItemModel.aggregate([{ $lookup: { from: 'order', localField: "order_id", foreignField: "order_id", as: "order_info" } }, { $match: { "_id":mongoose.Types.ObjectId('5bffa9bbd8cb3dff1d3d6658') } }], function (err, docs) { if (err) { console.log(err); return; } console.log(JSON.stringify(docs)) })