How to add an object into an array inside a collection in MongoDB using spring?

Viewed 76

I have a collection say, Employee

@Document(collection = "employee")
public class Employee{
  private int id;
  private int name;
  //setters and getters
}

I need to add a list in it , say jobHistory as shown below

@Document(collection = "employee")
public class Employee{
  @Id
  private int id;
  private int name;
  List<JobHistory> jobHostory;

  //setters and getters
}

And the jobHostory includes

public class jobHostory {
  private int id;
  private String jobtitle;
  private String companyName;
  private int noOfYears;
  
  //setters and getters
}

How can I add {"jobtitle" : "xxx", "companyName" : "xxx", "noOfYears" : "5"} into Employee Collection? And how the JobHistory id will autogenerate?

As I am new to MongoDB, I need some clarification further. Hope all understand. Thanks in advance. :)

0 Answers
Related