./model/db.js
// 引入mongoose const mongoose = require("mongoose"); //解决index索引引起的弃用提醒:(node:13616) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. mongoose.set("useCreateIndex", true); // 连接数据库 mongoose.connect("mongodb://127.0.0.1:27017/jtdb", { useNewUrlParser: true, useUnifiedTopology: true }, function (err, connection) { if (err) { console.log(err); return; } console.log("连接数据库成功"); }); module.exports = mongoose;
./model/order_item.js
const mongoose = require("./db.js"); var OrderItemSchema = mongoose.Schema({ order_id: String, title: String, price: Number, num: Number, }); module.exports = mongoose.model("Order_item", OrderItemSchema, "order_item");
order_item.js
/*查询order_item,找出商品名称是牛奶的商品,根据牛奶的订单号,再次查询对应订单的总价格*/ const mongoose = require("mongoose"); var OrderItemModel = require("./model/order_item.js"); var OrderModel = require("./model/order.js"); /* // 第一种实现方式 OrderItemModel.find( { _id: "60053a31e811c2445c3e43ea", }, function (err, orderItem) { // 先将对象转换为JSON格式【JSON.stringify(对象)】,之后又将格式重新转换为对象【JSON.parse(JSON格式文本)】,得到一个新的对象,从而实现深拷贝 var order_item = JSON.parse(JSON.stringify(orderItem));//深拷贝 if (err) return console.log(err); var order_id = order_item[0].order_id; OrderModel.find({ order_id: order_id }, function (err, order) { if (err) return console.log(err); order_item[0].order_info = order[0]; console.log(order_item); }); } ); */ // 第二种实现方式,aggregate聚合管道,用的语法还是原生的mongoDB语法 OrderItemModel.aggregate( [ { $lookup: { from: "order", localField: "order_id", foreignField: "order_id", as: "order_info", }, }, { $match: { _id: mongoose.Types.ObjectId("60053a31e811c2445c3e43ea"), }, }, ], function (err, docs) { if (err) return console.log(err); // ------------------------------------------------------------------------------------------------ console.log(docs[0]); // 输出以下: /* { _id: 60053a31e811c2445c3e43ea, order_id: '2', title: '金典牛奶', price: 10, num: 4, order_info: [ { _id: 60053a31e811c2445c3e43e2, order_id: '2', uid: '7', trade_no: '222', all_price: 140, all_num: 4 } ] } */ // ------------------------------------------------------------------------------------------------ // console.log(docs); // 输出以下---------- /* [ { _id: 60053a31e811c2445c3e43ea, order_id: '2', title: '金典牛奶', price: 10, num: 4, order_info: [ [Object] ] } ] */ // console.log(JSON.stringify(docs));//直接输出JSON格式文本-------------- /* [{"_id":"60053a31e811c2445c3e43ea","order_id":"2","title":"金典牛奶","price":10,"num":4,"order_info":[{"_id":"60053a31e811c2445c3e43e2","order_id":"2","uid":"7","trade_no":"222","all_price":140,"all_num":4}]}] */ // ------------------------------------------------------------------------------------------- // var order_info = docs[0].order_info; // console.log(order_info); // 输出以下: order_info /* [ { _id: 60053a31e811c2445c3e43e2, order_id: '2', uid: '7', trade_no: '222', all_price: 140, all_num: 4 } ] */ // ------------------------------------------------------------------------------------------ } ); // ObjectId("60053a31e811c2445c3e43ec");
执行 node ./order_item.js
输出:
{ _id: 60053a31e811c2445c3e43ea, order_id: '2', title: '金典牛奶', price: 10, num: 4, order_info: [ { _id: 60053a31e811c2445c3e43e2, order_id: '2', uid: '7', trade_no: '222', all_price: 140, all_num: 4 } ] }