I have the following entries in mongo db:
{ "_id" : { "t" : 1491329960, "id" : 2 } }
{ "_id" : { "t" : 1491329961, "id" : 2 } }
{ "_id" : { "t" : 1491329961, "id" : 3 } }
{ "_id" : { "t" : 1491329961, "id" : 3 } }
I need to find largest t for specific id
(assume test is collection and database names and specific id = 2)
In mongo client I need to run:
db.test.find( { '_id.id' : 2 } ).sort( { '_id.t' : -1} ).limit( 1 )
However, in pymongo, below doesn't work
from pymongo import MongoClient as mc
mc()['test']['test'].find( { '_id.id' : 2 } ).sort( { '_id.t' }, -1 )[0]
and raises:
TypeError: first item in each key pair must be a string
What should I do to get what I want using pymongo? Obviously, I can get everything and sort in python later, but that's wrong performance-wise
mc()['test']['test'].find( { '_id.id' : 2 }, sort = [ ( '_id.t', -1 )] )[0]ormc()['test']['test'].find_one( { '_id.id' : 2 }, sort = [ ( '_id.t', -1 )] )seems to work