Re: Feature request: created and updated timestamps

93 views
Skip to first unread message

Nicolas Clairon

unread,
Mar 29, 2011, 4:46:27 AM3/29/11
to mong...@googlegroups.com
Not yet. It's a bit tricky because atomic update are done directly via
the pymongo layer:

>>> con.MyDoc.collection.update({...}, {...})

So, for the moment, you should do it manualy. I may add an
`update_document` method so we can overwrite it...

For now, if you always use the `save()` method to update your
document, you can overwrite it.

N.

On Tue, Mar 29, 2011 at 12:44 AM, Wouter <w.b.er...@gmail.com> wrote:
> Hi All,
>
> Coming from relational databases and the plethora of ORMs available
> there is one feature I am missing above all. Automatic created and
> updated timestamps on documents.
>
> The created timestamp isn't an issue as you can use default_values for
> that, but the updated timestamp you have to remember to update
> manually.
>
> Is there any easier way to do this, any 'hooks' of some sort that I
> can plugin to?
>
> Thanks,
> Wouter

Nicolas Clairon

unread,
Mar 29, 2011, 4:49:25 AM3/29/11
to mong...@googlegroups.com
I fired a ticket for this issue. If you have any clean implementation
ideas, feel free do contribute to the discussion.

https://github.com/namlook/mongokit/issues#issue/47

Peter Bengtsson

unread,
Mar 29, 2011, 5:02:53 AM3/29/11
to mong...@googlegroups.com, Wouter
I would strongly advice against putting this inside MongoKit.
I personally almost always put something like this for all my documents:

class BaseDocument(Document):
structure = {
'add_date': datetime.datetime,
'modify_date': datetime.datetime,
}

default_values = {
'add_date': datetime.datetime.now,
'modify_date': datetime.datetime.now
}
use_autorefs = True
use_dot_notation = True


Setting the modify_date each time is easily done by overriding the save method.
If MongoKit was to implicitely set this Nicolas would need to know and
decide exactly how it should be called.

With over 10 years experience with SQL I'm a firm believer in being
explicit and to put as much logic as you can in the same layer (e.g.
Python).

On 28 March 2011 23:44, Wouter <w.b.er...@gmail.com> wrote:
> Hi All,
>
> Coming from relational databases and the plethora of ORMs available
> there is one feature I am missing above all. Automatic created and
> updated timestamps on documents.
>
> The created timestamp isn't an issue as you can use default_values for
> that, but the updated timestamp you have to remember to update
> manually.
>
> Is there any easier way to do this, any 'hooks' of some sort that I
> can plugin to?
>
> Thanks,
> Wouter

--
Peter Bengtsson,
home www.peterbe.com
fun donecal.com

Wouter

unread,
Mar 28, 2011, 6:44:04 PM3/28/11
to MongoKit

Nicolas Clairon

unread,
Mar 29, 2011, 5:38:17 AM3/29/11
to mong...@googlegroups.com
Hi Peter,

I totally agree with you. Especially with NoSQL, everything should be
explicit. For now, we can easily overwrite the save method but there's
no way to overwrite the "update" method in order to add more logic
each time we call it. Well, actually we can do it by overwriting the
Collection object but we can't create model related logic. Here's what
I have in mind :

class MyDoc(Document):
structure = {
'update_date': datetime,
}

def update_document(self, query, spec, **kwargs):
update_date = {'update_date': datetime.now()}
if '$set' in spec:
spec['$set'].update(update_date)
else:
spec['$set'] = update_date
super(MyDoc, self).update_document(query, spec, **kwargs)

this allow to inject some stuff in our atomic update query. Not only
to update_date (explicitly) but for multiple purpose...

Reply all
Reply to author
Forward
0 new messages