Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto generate IDs for MongoDB sub-documents in array with Spring MongoTemplate

My MongoDB document structure for a post is like this

{
"_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
    }
}

And there corresponding entity classes

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;

  ....

}

I am using Spring Data MongoTemplate for inserts. How can I configure MongoTemplate to auto generate _id for comments as well when they are inserted into post document, something like below

{
"_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
    }
}
like image 744
Meena Chaudhary Avatar asked Oct 22 '25 05:10

Meena Chaudhary


1 Answers

Spring Data maps your classes into MongoDB documents. During the mapping, only _id can be generated automatically.

MongoDB requires that you have an '_id' field for all documents. If you don't provide one the driver will assign a ObjectId with a generated value. A field annotated with @Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field.

A field without an annotation but named id will be mapped to the _id field.

https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping.conventions.id-field

Workaround:

Instance new ObjectId for id field and convention will convert id to _id.

public class CommentEntity {

    private String id;
    private LocalDateTime datePosted;
    private String comment;
    private int likes;

    ....

    public CommentEntity() {
        id = new ObjectId().toString();
        ...
    }

}
like image 101
Valijon Avatar answered Oct 23 '25 21:10

Valijon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!