Reputation: 750
i am trying to query some data using aggregation but something is not working...
I've been searching some examples and this is what i have...
( From my web app i just recieve a 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",
}
}
])
Then i need to use $lookup to join this collection and add configTarifas to the data, but i would like receive just the documents that match with the _id of the product filtered before...
what am i doing wrong? the config collection is not matching any $$producto... seemns like $$producto is not getting the _id value
Upvotes: 1
Views: 4236
Reputation: 13103
You have a mismatched types between parent $_id
(ObjectId
) field and idProducto
(String
). You need to convert ObjectId
to String
with $toString or $convert operators
{
$lookup: {
from: "configtarifas",
let: { "producto": {$toString: "$_id"} },
pipeline: [
{
$match: {
$expr: { $eq: ['$idProducto', "$$producto" ] },
}
}
],
as: "configs",
}
}
Upvotes: 4