【问题标题】:Creating relationship between collection in Pymongo and rendering it Jinja [duplicate]在 Pymongo 中创建集合之间的关系并渲染它 Jinja [重复]
【发布时间】:2018-10-24 04:40:32
【问题描述】:

考虑下图。我在 MongoDB 中有两个集合,我想通过匹配名称来呈现结果,它应该给我所需的输出。

我的问题是当我尝试这样编写代码时无法建立关系:

{% 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 %}

我真的被困在这里,不知道如何处理。如果您对我的问题有其他解决方案,请提供帮助。

【问题讨论】:

    标签: mongodb python-2.7 flask jinja2 pymongo


    【解决方案1】:

    我尽可能简单地回答了您的问题,以便您理解。你可以试试看。

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

    结果

    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
    

    【讨论】:

      【解决方案2】:

      您可以使用lookup stage 编写聚合查询,该查询执行与其他集合的左外连接。 请注意此功能是在 Mongo 3.2 中引入的。

      假设两个集合都是collection1 和collection2,然后从您的图表中绘制,您的查询将如下所示:

      pipeline = [
          {
              '$lookup': {
                  'from': 'collection2',
                  'localField': 'Product',
                  'foreignField': 'Product',
                  'as': 'Matches'
              }
          }
      ]
      
      db.collection1.aggregate(pipeline)
      

      在 Jinja 中,你可以这样写:

      {% 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 %}
      

      【讨论】:

      • 您的解决方案部分对我有用。
      猜你喜欢
      • 2017-10-18
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 2014-11-13
      • 1970-01-01
      相关资源
      最近更新 更多