Enterprise Java

Auditing Entities in Spring Data MongoDB

Spring Data MongoDB 1.2.0 silently introduced new feature: support for basic auditing. Because you will not find too much about it in official reference in this post I will show what benefits does it bring, how to configure Spring for auditing and how to annotate your documents to make them auditable Auditing let you declaratively tell Spring to store:


First of all Maven dependencies to latest Spring Data MongoDB and Spring Data Commons. Additionally in order to use date-related audit annotations we need to add joda-time to classpath.




In order to enable auditing we need to add <mongo:auditing/> to Spring configuration. Currently there is no way to configure it through Java Config.

<mongo:auditing />

<mongo:mongo id="mongo" />

<bean class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="blog-tests" />


Configuration above provides us way for auditing that includes versioning and timestamps. Example document will look like:

public class Item {
    private String id;


    private Long version;
    private DateTime createdAt;
    private DateTime lastModified;


Now you can save document using MongoTemplate or your repository and all annotated fields are automagically set.

As you have probably noticed I did not use here user related annotations @CreatedBy and @LastModifiedBy. In order to use them we need to tell Spring who is a current user.

First add user related fields to your audited class:

private String createdBy;

private String lastModifiedBy;

Then create your implementation of AuditorAware that will obtain current user (probably from session or Spring Security context – depends on your application):

public class MyAppAuditor implements AuditorAware<String> {

    public String getCurrentAuditor() {
        // get your user name here
        return "John Doe";

Last thing is to tell Spring Data MongoDB about this auditor aware class by little modification in Mongo configuration:

<mongo:auditing auditor-aware-ref="auditor" />
<bean id="auditor" class="pl.maciejwalkowiak.blog.MyAppAuditor"/>


Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
8 years ago

Way better than the current documentation (1.7.0.RC1). Thanks!

6 years ago

Thank you for this post, it set me on track to set up user auditing.

Just so you know, your post “inspired” someone, word for word : http://programmerpractice.blogspot.fr/2015/07/auditing-entities-in-spring-data-mongodb.html

Back to top button