I am trying to sort a list of students from a table in mongodb. I am using tornado and python. I am using Motorclient
to connect with db .I am getting a proper result when using students.find()
Sid = self.body['Sid']
data = []
_id = db.students.find({"Sid": Sid},{'_id': False,"status": False,"Dateofadmission":False})
for document in (yield _id.to_list(length=100)):
data.append(document)
return[{"status code": 1,"studentInfo": data }]
Now when I try to sort and list it gives me internal server error, with no error logging in the terminal.
_id = db.students.find({"Sid": Sid},{'_id': False,"status": False,"Dateofadmission":False}).sort({'Dateofadmission' : -1})
the date is stored in mongodb as:
{
"_id" : ObjectId("56443dc03f32df1bf0e8b4e8"),
"Dateofadmission" : ISODate("2015-10-22T00:00:00Z"),
"Sid" : "56443dc03f32df1bf0e8b4e8",
"Name" : "Ram"
}
Someone please guide me on how I can sort the list of students based on the Dateofadmission
sort()
in pymongo
takes two parameters - a key (or a list of keys) and the direction. Replace:
db.students.find({"Sid": Sid}, {'_id': False,"status": False,"Dateofadmission":False}).sort({'Dateofadmission' : -1})
with:
db.students.find({"Sid": Sid}, {'_id': False,"status": False,"Dateofadmission":False}).sort('Dateofadmission', pymongo.DESCENDING)