I have a collection of items in mongodb that look like this (I'm using pymongo):
{'type':'_sometype', 'name':'_somename', 'date':datetime, 'incoming':bool}
type
and name
are not unique across items, so it is possible to have items with the same type
and name
but differing date
s.
The issue: sometimes I'll need to determine if an item retrieved in some query is the latest version of that item. What I've been doing is this:
def isLatest(item):
return not collection.find_one({'date':{'$gt':item['date']},
'type':item['type'],
'name':item['name'],
'incoming':item['incoming']})
Which simply queries mongodb for a matching item that has a more recent value for date
. If such an item is found, this function returns False
(because the item
passed in is not the latest version).
I call this function many times, once for each item in a given query, and it seems to me like there'd be a much better way of going about this than repeatedly querying for some data. Unless this looks OK to everybody, in which case I thank you for your evaluation :D
Any thoughts appreciated, thanks in advance,
-S