These are chat archives for Automattic/mongoose

4th
Oct 2018
Gerald
@campanagerald
Oct 04 2018 04:16
is there a way not to cast to ObjectId when using an aggregate?
subhashish negi
@subhashish12
Oct 04 2018 05:47
    productModel.aggregate( [ 
        {$match:{ txtProductName: { $regex:regexStr}} },
        {
            $lookup: {
              from: 'productSeller',
              localField:'_id',
              foreignField: 'productId',
              as: 'productDetails'
            }
        }
    ]).exec((err,resp)=>{
        if(err) {
            res.status(400).send('error')
        }
        else {
            res.status(200).send(resp);
        }
    })
in resp i am getting results and array 'productDetails'. IN productDetails i have a parameter as id according to that id i have to find name from another collection is it possible by aggregation itself ?
Kev
@lineus
Oct 04 2018 09:25
@campanagerald mongoose doesn't cast values in aggregate ( or at least it shouldn't be )
@subhashish12 it sounds like you need to add another $lookup stage, maybe with an $unwind stage before it on the array path that contains the localField. It's hard to say without seeing a more complete example ( schema, example docs )