0

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.

alexmngn
  • 9,107
  • 19
  • 70
  • 130
  • Possible duplicate of [Group array after unwind and match](http://stackoverflow.com/questions/30119575/group-array-after-unwind-and-match) – TomG Oct 05 '16 at 08:17
  • Take a look here, mongodb documentation is really helpfull : https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/ – Steeve Pitis Oct 05 '16 at 08:22
  • @SteevePitis I've been on it for some time now, but I still can't figure it out. – alexmngn Oct 05 '16 at 08:26
  • Aggregate is really hard at the beginning, what I've done to understand this, is to go step by step and log the result after each step. – Steeve Pitis Oct 05 '16 at 08:38
  • It would go a long way in attracting good answers especially with aggregation type queries if you could show some sample documents from both collections and your expected output from that sample. – chridam Oct 05 '16 at 09:01

0 Answers0