I want to automatically update a LastUpdatedOn field on a mongo document every time a save or update operation occurs on that document.
Rather than putting this burden on every bit of code that does a save/update, is there some way to automatically do this (I’m using the C# driver).
For e.g. in nHibernate you can use interceptors. Is there a similar technique available at either the database or driver level?
There is no such mechanism in mongodb c# driver and in the mongodb as well.
To achieve your needs you can simple wrap all
Save,Insert, atomicUpdatemethods and setLastUpdatedOnfield there.For example you can have base class for your repository, service, whatever where you will wrap your Save methods:
then you will need inherit all your documents from
BaseDocument(or force them implement some interface) to add constraint that all documents should contains LastUpdatedOn field.Then just inherit all data access classes from above one and use Save, Update methods from base class.
Note: all this code was not tested, it just how i see it can be done.