tags:

views:

30

answers:

2

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 dates.


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

A: 

Without knowing any more details about your application than what you've provided, it seems to me it might be better to have a separate collection that only stores the latest version and is indexed by type-name; and a separate collection for historical versions.

Rex M
A: 

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

To be honest if you need to keep checking to see if it is the latest, you are going to need to issue queries. That's unavoidable - the act of needing to know necessitates a query.

If querying the database frequently is not bothering you (i.e. causing some other difficulties or complications), then I see nothing wrong. If it is causing problems, it might be the design of the application itself which needs re-examining. Without knowing more about what it is you're trying to achieve, its pretty difficult to say. Personally though, I'd say if everything is looking good, you've probably got nothing to worry about. As always, just make sure your query isn't any less efficient than it needs to be.

Splash