I recently had to write a test where I needed to manually set the value of a field annotated with @LastModifiedDate.

This proved difficult, as after much searching, I found that there was no way to selectively disable @EnableMongoAuditing once it had been enabled as part of some configuration. 

However, it appears that there is a loophole in the auditing behavior: if you perform a direct update query, you can successfully set your own custom values. It seems that this direct Mongo-level operation is beyond the scope of Spring’s auditing, and thus it can be used in exceptional situations where different behavior is required.

Furthermore, since this workaround does not require any configuration changes, it can be used in production code as well!

Here's some test code that demonstrates this behavior. In it, you can see that the values supplied on a normal insert and save are overwritten by the auditor, but the update query is able to successfully circumvent it.

public class MongoAuditingBypassTest {

    public static class Config {}

    public static class MyDocument {
        private String id;

        private String name;

        private Instant lastModified;

    MongoOperations mongoOperations;


    void clearDb() {
        mongoOperations.remove(new Query(), MyDocument.class);


    public void mongoAuditingDemonstration() {
        MyDocument myDocument = new MyDocument();
        myDocument.setName("My Document!");

        MyDocument inserted = mongoOperations.insert(myDocument);
        assertEquals(myDocument.getName(), inserted.getName());
                .isCloseTo(Instant.now(), within(5L, ChronoUnit.SECONDS));

        // try doing a normal save of the record with a new date

        MyDocument updated = mongoOperations.save(inserted);
                .isCloseTo(Instant.now(), within(5L, ChronoUnit.SECONDS));

        // custom update query

        MyDocument manuallyUpdated =
                mongoOperations.findAndModify(new Query(Criteria.where("id").is(inserted.getId())),

                        new Update().set("lastModified", Instant.EPOCH),
        assertEquals(Instant.EPOCH, manuallyUpdated.getLastModified());