1

Consider the below image. I have two collection in MongoDB and I want to render the result where just by matching the name it should give me the desired output.

My problem is I am unable to establish the relationship when I try to code like so:

{% for i in new %}
    <h4 class>New Price - {{ i.Product }}</h4>
        {% for z in Preowned %}
            {% if z['Product'] == i.Product %}
            <h4 class>Preowned Price - {{z.Price}}</h4>
            {%endif %}
{% endfor %}
{% endfor %}

I am really stuck here and not sure how to handle this. If you have alternate solution for my problem then please help.

enter image description here

fear_matrix
  • 4,912
  • 10
  • 44
  • 65

2 Answers2

1

I answered your problem as simple as I can for you to understand it. You can try and test it out.

app.py

@app.route('/test')
def test():
    New = db.New.find().sort('Product', 1)
    Preowned = db.Preowned.find().sort('Product', 1)
    return render_template('test.html',
                           NP=zip(list(New), list(Preowned)))

NP = zip(list(New), list(Preowned))

[

({'_id': ObjectId('5af9bd331f2d0f3b97f57ee3'), 'Product': 'Adidas', 'Price': 2000, 'Platform': 'Shoes'},
{'_id': ObjectId('5af9bda51f2d0f3b97f57f1c'), 'Product': 'Adidas', 'Price': 1000, 'Platform': 'Shoes'}),

({'_id': ObjectId('5af9bd601f2d0f3b97f57efd'), 'Product': 'Fila', 'Price': 2400, 'Platform': 'Shoes'},
{'_id': ObjectId('5af9bdcb1f2d0f3b97f57f2b'), 'Product': 'Fila', 'Price': 400, 'Platform': 'Shoes'}),

({'_id': ObjectId('5af9bd751f2d0f3b97f57f05'), 'Product': 'Puma', 'Price': 700, 'Platform': 'Shoes'},
{'_id': ObjectId('5af9bdd91f2d0f3b97f57f33'), 'Product': 'Puma', 'Price': 400, 'Platform': 'Shoes'}),

({'_id': ObjectId('5af9bd4f1f2d0f3b97f57ef4'), 'Product': 'Sneakers', 'Price': 1600, 'Platform': 'Shoes'},
{'_id': ObjectId('5af9bdbd1f2d0f3b97f57f27'), 'Product': 'Sneakers', 'Price': 600, 'Platform': 'Shoes'})

]

test.html

{% for n, p in NP %}
    {% if n.Product == p.Product %}
        <h4>{{n.Product}} {{n.Platform}}</h4>
        <h4>Buy Now - New : {{n.Price}}</h4>
        <h4>Buy Now - Preowned : {{p.Price}}</h4>
        <br>
    {% endif %}
{% endfor %}

result

Adidas Shoes
Buy Now - New : 2000
Buy Now - Preowned : 1000

Sneakers Shoes
Buy Now - New : 1600
Buy Now - Preowned : 600

Fila Shoes
Buy Now - New : 2400
Buy Now - Preowned : 400

Puma Shoes
Buy Now - New : 700
Buy Now - Preowned : 400
Ninja Warrior 11
  • 362
  • 3
  • 17
1

You can write an aggregation query with a lookup stage that performs a left outer join to some other collection. Note that this feature was introduced in Mongo 3.2.

Say that both collections are collection1 & collection2, then drawing from your diagramming, your query will look like:

pipeline = [
    {
        '$lookup': {
            'from': 'collection2',
            'localField': 'Product',
            'foreignField': 'Product',
            'as': 'Matches'
        }
    }
]

db.collection1.aggregate(pipeline)

In Jinja, you can write:

{% for new_item in new_items %}
    <h4>New Price - {{ new_item.Product }}</h4>
    {% for preowned in new_item.Matches %}
        <h4>Preowned Price - {{ preowned.Price }}</h4>
    {% endfor %}
{% endfor %}
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81