How to enable auditing for MongoDB via annotations in Spring

I wanted to enable some audit features like @CreatedDate. I do not use the Spring xml configuration file, so I cannot add mongo: auditing to Spring configuration. I was wondering if there is an alternative way to enable auditing. The following code is a model for the user. But whenever I create a user, the date is not saved in the document, so the audit does not work. Can anyone help me out?

@Document(collection = "user")
public class User {
    @Id
    private String id;
    @Indexed(unique = true)
    private String email;
    private String name;
    @CreatedDate
    private Date date;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
+3
source share
3 answers

Since you are not using configuration via XML, I believe that you are using annotations. You have a class like this:

public class MongoConfig extends AbstractMongoConfiguration {...}

, , , : @EnableMongoAuditing

:

@Configuration
@EnableMongoRepositories(basePackages="...")
@EnableMongoAuditing
public class MongoConfig extends AbstractMongoConfiguration {...}

, !

+5

, MongoDB mongoClient, db url. @EnableMongoAuditing .

0

That is all you need. No subclasses or other things.

@Configuration
@EnableMongoAuditing
public class AnyCongig {}
0
source

All Articles