Friday, 26 May 2017

mongodb $lookup in array inside array

Hi i have an order collection like this

{_id :ObjectId(......),
  productDetails:[{_id:ObjectId(....),
                   name: nokia,
                   qunatity:2,
                   products:[{productId:ObjectId(.....)},
                             {productId:objectId(.....)}],
                   _id:objectId(....),
                   name: motorola,
                   quantity:2,
                   products:[{productId:objectId(......)},
                             {productId:objectId(.....)}]
                 ]}
   }

and the products collection is

{_id:objectId(.....),
 createdAt: date,
 products:[{_id:objectId(....),
            name:nokia,
            model:v1}]
 }

My query is Order.aggregate([ { $match: { _id: req.params.id } }, { $unwind: "$productDetails.products" }, { $lookup: { from: "Product", localField: "productDetails.products", foreignField: "products._id", as: "products_docs" } }, { $match: { "products_docs": { $ne: [] } } } ], function(err, result) { console.log(result); return res.json(result); });

Which is not working(getting empty array) , can anyone please help me to solve this?



via Manoj

No comments:

Post a Comment