0

how do i sort nested documents:

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

this dont work!

here is my schema:

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

everything worked without sorting, i just use produits = self.db.users.find_one({"email":email})["produit_up"] and in my templates i just call :

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

so how do i do the same if i use SORT and LIMIT and then return the namep for example or prix

Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65

2 Answers2

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)
Lujeni
  • 356
  • 2
  • 10
  • thank you, but finally, it seems that it's only made using Agregate http://groups.google.com/group/mongodb-user/browse_thread/thread/1b38ecae19ef71af/abfb902771315456?lnk=gst&q=alabdelouahab#abfb902771315456 – Abdelouahab Pp Sep 06 '12 at 21:57
0

ok, the tip is to use the python list, and then, sort it with a key How do I sort a list of dictionaries by values of the dictionary in Python?

Community
  • 1
  • 1
Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65