Home » Java » Enterprise Java » Auditing entities in Spring Data MongoDB

About Maciej Walkowiak

Maciej Walkowiak

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"/>


(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
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 ....
I agree to the Terms and Privacy Policy
Notify of

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

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
5 months ago

The created date is missing on update. Is there a way to keep persisting this on update?