javascript - 如何在猫鼬上聚合填充字段
问题描述
我需要将机器(maquinas)聚合到我的 Schema Producto
let ProductoFinal = new Schema([{
cliente:{
type:Schema.Types.ObjectId,
ref: 'cliente'
},
grupo:{
type:Schema.Types.ObjectId,
ref: 'grupo'
},
nombre :{
type:String
},
materiales: {
type:Array
}
}]);
//This is my productos Schema and I populated the grupo
let GrupoSchema = new Schema([{
nombre:{
type:String,
required:true
},
tipos:{
type:Array,
required:true
}
}]);
我用相同的 ID 用 grupos 填充了 Schema productos
Producto.findById(id)
.populate('grupo')
.exec((err, productosDB)=>{
if( err ){
return res.status(400).json({
ok:false,
err
});
}
res.json({
producto:productosDB
})
})
grupo 的tipo 是一个数组,我想从这个聚合所有具有相同tipo 的maquinas,我需要一个名为equipo 的字段,但是当我在字段grupo 中聚合时,会出现错误并且无法显示它
let MaquinaSchema = new Schema([{
nombre:{
type:String,
required:true
},
tipo:{
type:String,
required:true
},
colores:{
type:Number
},
cph:{
type:Number,
required:true
}
}]);
我试过这个但不起作用
Producto.findById(id)
.populate('grupo')
// .aggregate([
// {
// $lookup:{
// from:'maquinas',
// let:{
// funcionMaquinas:"$grupo.tipos"
// },
// pipeline:[
// {
// $match:{
// $expr:{
// $in:["$tipo","$$funcionMaquinas",]
// }
// },
// }
// ],
// as:'equipo'
// }
// }
// ])
.exec((err, productosDB)=>{
if( err ){
return res.status(400).json({
ok:false,
err
});
}
res.json({
producto:productosDB
})
})
解决方案
推荐阅读
- laravel-5 - Laravel 5 队列
- node.js - UnhandledPromiseRejectionWarning:错误:协议错误(Network.setCookies):目标已关闭 - 无法设置 cookie
- android - 什么是idea文件夹的codeStyles中的codeStyleConfig xml文件?
- unix - 寻找匹配的'}'时出现意外的EOF
- vb.net - 如何编辑此消息框?
- scala - 使用 spark dataframe 列值作为另一列的别名
- javascript - 在 Java Script 中将日期格式从一种格式更改为另一种格式
- android - 未检测到在 Onesignal NotificationExtenderService 中将待定意图设置为 Builder
- maven - 番石榴不向后兼容
- php - 为什么 bigcommerce 的 api 中没有购物车级别的折扣?