Glasnhost
Glasnhost

Reputation: 1135

How do you write a springboot class for a mongodb document with nested properties?

I have a json document

{
 description:"desc", 
 user:{name:"Name", email:"email"}, 
 properties:[
      {name:"propName1",val:"propValue1"},
      {name:"propName2",val:"propValue2"}
      ]
}

I defined the class in spring framework:

@Document(collection = "mydocuments")
public class MyDocument extends BaseDocument {
    private String description;
    ....?
}

How do I define user and properties?

Upvotes: 1

Views: 348

Answers (1)

varman
varman

Reputation: 8894

You can simply write two classes and create the instance inside the MyDocument class.

class User{
    String name;
    String email;
    
    // Constructors getters and setters
}

class Property{
    String name;
    String val;
    
    // Constructors getters and setters
}

@Document(collection = "mydocuments")
public class MyDocument extends BaseDocument {
    private String description;
    private User user;
    private List<Property> properties;
}

Upvotes: 1

Related Questions