问题场景
springboot项目中操作mongodb对Document进行插入操作,对其数组字段对象中的元素使用自动填充
测试Document对象
@Data
@Document
public class TestDocument {
@MongoId
private String id;
private List<TestField> fields;
}
@Data
public class TestField {
@MongoId
private String id;
private String name;
@CreatedDate
private LocalDateTime time;
}
问题描述:
TestDocument testDocument = new TestDocument();
TestField testField = new TestField();
testField.setName("测试");
testDocument.setFields(Collections.singletonList(testField));
System.out.println(mongoTemplate.save(testDocument));
插入操作返回的数据未能自动填充
MongoDB存储数据
{
"_id": "6414466c347e397d7d393b0e",
"fields": [
{
"name": "测试"
}
]
}
提问
Document内嵌List字段中的元素是否能使用自动填充?需要怎么配置内嵌List字段中的元素才能使用自动填充?