0

Consider the below three collection in which I have grouped Collection 1 with Collection 2 using $lookup. I am able to get the preowned price for Adidas shoes but when a user selects PUMA shoes then Adidas preowned shoes is also displayed (tried using $match) because product name is similar in all 3 collections. What i want is by using same aggregate function I want to display result if the Platform is ADIDAS and if not ADIDAS then it should not display preowned result for PUMA.

I can handle this by just using if else statement in my Jinja template but I don't want to do that. I want to handle this stuff in my aggregate function

So the function goes like this

  • User selects ADIDAS shoes
  • If preowned shoes for ADIDAS exists in collection 2 with Platform ADIDAS then display preowned price
  • If not then don't display Adidas preowned shoes for PUMA

enter image description here

Below is the code I am using in my pymongo. I have specified a $match on my Platform but it doesn't seems to work.

  db.collection1.aggregate([
                {'$match': {'Platform':{'$eq':[variable]}}},
                {'$lookup':{
                    'from': 'collection2',
                    'localField': 'Product',
                    'foreignField': 'Product',
                     'as': 'Matches'}} 
                 ])

Variable here is the input that I want to give.

Below is the data if someone wants to test

Collection 1 - New
Product Price Platform Sneakers 800 Adidas Boots 700 Adidas Running-Shoes 600 Adidas

Collection 2 - Preowned
Product Price Platform Sneakers 300 Adidas Boots 100 Adidas Running-Shoes 200 Adidas

Collection 3 - New
Product Price Platform Sneakers 900 Puma Boots 600 Puma Running-Shoes 600 Puma

My collection structure is like so :

{ 
    "_id" : ObjectId("5af963c351f4944d713484da"), 
    "Platform" : "Adidas", 
    "Price" : NumberInt(2000), 
    "Product" : "Sneakers"
}
fear_matrix
  • 4,912
  • 10
  • 44
  • 65
  • Please remove the pictures and show real documents from your collection. People cannot reproduce an issue from pictures and we require data to work from instead. Also there needs to be some realistic attempt and an actual issue. Otherwise you are just looking for an example to follow and there are already answers that do that. – Neil Lunn May 21 '18 at 06:14
  • No you have not made sufficient changes. Show documents as they appear in your actual collections with enough data to reproduce a desired sample result. General "how do I join" is already long answered. Read the exsiting answers if you have nothing more to ask, or actually give a reproducible issue here. I see none. – Neil Lunn May 21 '18 at 06:19

0 Answers0