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
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" } 