When saving an audited (@CreatedDate, @LastModifiedDate) document with nested audited documents, the two dates will be also reflected in nested docs.
This is the scenario:
DocumentA.java
public class DocumentA {
@Id
private String id;
@Version
private Long version;
@CreatedDate
private Long createdDate;
@LastModifiedDate
private Long lastModifiedDate;
// getters and setters
}
DocumentB.java
public class DocumentB {
@Id
private String id;
@Version
private Long version;
@CreatedDate
private Long createdDate;
@LastModifiedDate
private Long lastModifiedDate;
private DocumentA docA;
// getters and setters
}
DocumentA is already stored in db having its createdDate and lastModifiedDate set. Then, when saving new DocumentB with nested DocumentA, the 2 dates of nested DocumentA will be modified to the same values just set for DocumentB. This only happen in nested document, while stored DocumentA is not touched (luckily!). The expected behaviour is that nested document will remain exactly the same just set via code (it means the same of the original documentA)
This is working like it is design.
Embedding the document A is not same as having the reference to document A. Embedding document is managed as part of main document means all the changes are tracked as they are top level fields in document B. Referencing document it is tracked and managed separately.
If you are only referencing you should either use manual reference and load using separate call or using $lookup aggregation query. Other alternative would be to use dbref to have the driver load the referenced document when it is loading the main document.