Моя структура документа MongoDB для post
выглядит следующим образом
{
"_id" : ObjectId("5e487ce64787a51f073d0915"),
"active" : true,
"datePosted" : ISODate("2020-02-15T23:21:10.329Z"),
"likes" : 400,
"commentList" : [
{
"datePosted" : ISODate("2020-02-15T23:21:10.329Z"),
"comment" : "I read all your posts and always think they don't make any sense",
"likes" : 368
},
{
"datePosted" : ISODate("2020-02-15T23:21:10.329Z"),
"comment" : "I enjoy all your posts and are great way to kill time",
"likes" : 3533
}
}
И существуют соответствующие классы сущностей
CommentEntity. java
public class CommentEntity{
private String id;
private LocalDateTime datePosted;
private String comment;
private int likes;
....
}
PostEntity . java
@Document(collection = "post")
public class PostEntity {
@Id
private String id;
private boolean active;
private LocalDateTime datePosted;
private int likes;
private List<CommentEntity> commentList;
....
}
Я использую Spring Data MongoTemplate
для вставок. Как настроить MongoTemplate
на автоматическое создание _id
для комментариев, а также при их вставке в документ post
, что-то вроде ниже
{
"_id" : ObjectId("5e487ce64787a51f073d0915"),
"active" : true,
"datePosted" : ISODate("2020-02-15T23:21:10.329Z"),
"likes" : 400,
"commentList" : [
{
"_id" : ObjectId("5e487ce64787a51f07snd315"),
"datePosted" : ISODate("2020-02-15T23:21:10.329Z"),
"comment" : "I read all your posts and always think they don't make any sense",
"likes" : 368
},
{
"_id" : ObjectId("5e48764787a51f07snd5j4hb4k"),
"datePosted" : ISODate("2020-02-15T23:21:10.329Z"),
"comment" : "I enjoy all your posts and are great way to kill time",
"likes" : 3533
}
}