I need to insert a new track into the existing event document following is my class structure
class Event
{
String _id;
List<Track> tracks;
}
class Track
{
String _id;
String title;
}
My existing document is
{
"_id":"1000",
"event_name":"Some Name"
}
document will look like after insertion
{
"_id":"1000",
"event_name":"Some name",
"tracks":
[
{
"title":"Test titile",
}
]
}
How can i insert that track into my existing document using mongoTemplate spring data mongodb?
First, you have to annotate
Event
class with@Document
:The code for adding an event should look like this:
Note : You should change
Event
's classString _id;
toString id;
in order for this example to work (or change the query literal).Edit update a track is also fairly easy. Suppose you want to change the first track's title: