【发布时间】:2020-07-12 03:33:30
【问题描述】:
我正在尝试使用聚合查询一些数据,但有些东西不起作用...
我一直在寻找一些例子,这就是我所拥有的......
(从我的网络应用程序中我刚刚收到了一个 searchTerm)
productosModel.aggregate([
//IN THE FIRST STAGE I FILTER BY REF OR NAME ( searchTerm )
{
$match: {
$or: [
{ ref: { '$regex': req.body.ref, '$options': 'i' } },
{ nombre: { '$regex': req.body.nombre, '$options': 'i' } }
]
}
},
{
$lookup: {
from: "configtarifas",
let: { "producto": "$_id" }, //HERE IS WHERE I PRETEND TO DEFINE THE _ID FILTERED
pipeline: [
{
$match: {
$expr: { $eq: ['$idProducto', "$$producto" ] },
}
}
],
as: "configs",
}
}
])
然后我需要使用 $lookup 加入此集合并将 configTarifas 添加到数据中,但我希望只接收与之前过滤的产品的 _id 匹配的文档...
我做错了什么?配置集合不匹配任何 $$producto...似乎 $$producto 没有获得 _id 值
【问题讨论】:
-
可能
idProducto是String 类型,而$$producto是ObjectId 类型。只需这样转换:let: { "producto": {$toString: "$_id"} } -
@Valijon jesus...真是个错误...是的,它正在起作用...如果您愿意,请将其添加为解决方案...谢谢!
标签: mongodb aggregation-framework