I'm starting with MongoDB and it gets a bit complicated with what I need. I have 2 mongodb collections: Products and ProductDetails
There is a relation 1 to many between those two collections. One product can have to multiple product details (one per user actually).
I'm trying to retrieve the list of products for a specific user that doesn't have details yet.
I found the $lookup
and $unwind
operators but I need the reverse of this
My current call is not really what I need, but I'm not sure how to place the userId, neither if I'm going in the right direction with that. This is how it looks like:
var userId = '1234abcd'; // Not used yet
// Products is my first collection
products.aggregate([
{
$lookup: {
from: 'product-details',
localField: '_id',
foreignField: 'product',
as: 'productDetails',
}
},
{
$unwind: '$productDetails',
},
], function (err, result) {
// I need to get here the list of products the user doesn't have
// This currently returns the entire list of products that has details. Not really what I need...
});
Do you know how I could get to what I need? Thanks.