python - pymongo sorting by date -
i want newest posts first, , try following:
db.posts.find({"date": {"$lt": tomorrow, "$gte": today}}).sort({'date':pymongo.descending})
(without sort, oldest posts first fine)
i getting error
typeerror: if no direction specified, key_or_list must instance of list
what going on here? not possible sort date?
this not correct format of parameters sort
function. correct syntax this:
db.posts.find(...).sort('date',pymongo.descending)
here link relevant documentation sort
function: http://api.mongodb.org/python/current/api/pymongo/cursor.html#pymongo.cursor.cursor.sort
to sort multiple parameters can use following syntax:
db.posts.find(...).sort([ ('date', pymongo.ascending), ('other_field', pymongo.descending) ]):
Comments
Post a Comment