【问题标题】:sorting nested documents with pymongo使用 pymongo 对嵌套文档进行排序
【发布时间】:2012-09-08 05:44:31
【问题描述】:

如何对嵌套文档进行排序:

produits = self.db.users.find({"email":email}, {"produit_up":1,"_id":0}).sort(("produit_up.namep"), pymongo.DESCENDING)

这行不通!

这是我的架构:

user={
"pseudo":pseudo,
"email":email, 
"avatar":avatar_id
"produit_up":{
"namep":namep, 
"prix":prix, 
}
}

一切都没有排序,我只使用 produits = self.db.users.find_one({"email":email})["produit_up"] 在我的模板中我只是调用:

{% for produit in list(produits) %}
{{ produit["namep"] }}  
{{ produit["prix"] }}
....
{% end %}

如果我使用SORTLIMIT 然后返回namepprix,我该怎么做?

【问题讨论】:

    标签: python tornado pymongo


    【解决方案1】:
    # find_one document with sort.
    db.produit.find_one({"email": "email"}, { "produit_up" : 1,"_id" : 0 }, sort=[("produit_up.namep", 1)])
    
    # find all document with sort and limit at last 10 document.
    db.produit.find({"email": "email"}, { "produit_up" : 1,"_id" : 0 }, sort=[("produit_up.namep", 1)]).limit(10)
    

    【讨论】:

    【解决方案2】:

    好的,提示是使用python列表,然后用key排序 How do I sort a list of dictionaries by values of the dictionary in Python?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 2012-03-21
      • 2021-11-19
      • 1970-01-01
      相关资源
      最近更新 更多