问题
I have an orderSchema
that has a subdoc productOrder
:
const paymentTrans = require('./PaymentTrans').schema;
const productOrder = require('./ProductOrder').schema;
const orderSchema = new mongoose.Schema({
orderId: { type: mongoose.Schema.Types.ObjectId },
userId : {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
productOrder: [productOrder],
totalPerOrder: {type: Number, default:''},
paymentTrans: [paymentTrans]
}, { timestamps: true });
const Order = mongoose.model('Order', orderSchema);
module.exports = Order;
porductOrder Schema
const productOrderSchema = new mongoose.Schema({
sku: { type: String, default: '' },
name: { type: String, default: '' },
quantity: { type: Number, default: '' },
price: { type: Number, default: '' },
total: { type: Number, default: '' }
});
const ProductOrder = mongoose.model('ProductOrder', productOrderSchema);
module.exports = ProductOrder;
I wish to get productOrder
from order
(orderSchema
):
tried these
Order.findById(req.body.id)
.exec(function (err, products) {
var opts = {
path: 'productOrder',
model: 'ProductOrder'
};
Order.populate(products, opts, function (ree, products) {
console.log(JSON.stringify(products));
});
res.render('store/cart', {
title: 'MyCart',
angularApp: 'storeApp',
products: products
});
});
Where req.body.id
is the orderId
and the object
in console
looks like this: (sorry, WebStorm
no Copy/Paste from console)
Another way I tried
Order.findById(req.body.id, function (err, products) {
var opts = [
{ path: 'productOrder', model: 'ProductOrder' }
]
Order.populate(products, opts, function (err, products) {
console.log(products);
})
})
And yet another
Order.find({ _id: req.body.id, userId: req.user._id })
.populate({
path: 'productOrder',
populate: {
path: 'productOrder',
model: 'ProductOrder'
}
})
.exec(function (err, productOrder) {
//products.forEach(function(elem){
console.log("products = " + JSON.stringify(productOrder));
//});
console.log("REQ._ID " + req.body.id);
res.render('store/cart', {
title: 'MyCart',
angularApp: 'storeApp',
products: productOrder
});
});
They all end up giving me the entire doc
of the order.
How can I get just the products of the order? (productOrder
)
Thanx
回答1:
You may use select
function and then populate as following
Order.findOne({ _id: req.body.id})
.select('-_id productOrder')
.populate('productOrder')
.exec(function (err, orderObj) {
if(!err) {
var productOrder = orderObj.productOrder;
//products.forEach(function(elem){
console.log("products = " + JSON.stringify(productOrder));
//});
console.log("REQ._ID " + req.body.id);
res.render('store/cart', {
title: 'MyCart',
angularApp: 'storeApp',
products: productOrder
});
}
});
来源:https://stackoverflow.com/questions/39570938/mongoose-populate-subdoc