title: Auto generate IDs for MongoDB sub-documents in array with Spring MongoTemplate
body: <p>My MongoDB document structure for a <code>post</code> is like this</p>
  
  <pre><code>{
  "_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
      }
  }
  </code></pre>
  
  <p>And there corresponding entity classes</p>
  
  <p>CommentEntity.java</p>
  
  <pre><code>public class CommentEntity{
  
    private String id;
    private LocalDateTime datePosted;
    private String comment;
    private int likes;
  
    ....
  
  }
  </code></pre>
  
  <p>PostEntity.java</p>
  
  <pre><code>@Document(collection = "post")
  public class PostEntity {
  
    @Id
    private String id;
    private boolean active;
    private LocalDateTime datePosted;
    private int likes;
    private List&lt;CommentEntity&gt; commentList;
  
    ....
  
  }
  </code></pre>
  
  <p>I am using Spring Data <code>MongoTemplate</code> for inserts. How can I configure <code>MongoTemplate</code> to auto generate <code>_id</code> for comments as well when they are inserted into <code>post</code> document, something like below</p>
  
  <pre><code>{
  "_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
      }
  }
  </code></pre>