Newbie Alert !
I just installed mongodb 2 days back and started creating REST api's over spring.
So i have a collection, userinfo, where a sample document would look like
{"_id":"5c62588e5e1fbc37dc9746d3","name":{"first":"rajan","last":"rawat"},"age":32}
I created the field name as Object type in the collection.
Now creating the entity class for it in java
@Document(collection = "userinfo")
public class UserInfo {
@Id
private String id;
private Name name;
private int age;
}
where the Class Name is
public class Name {
private String firstName;
private String lastName;
}
On running the API, the response I get is
{"id":"5c62588e5e1fbc37dc9746d3","name":{"firstName":null,"lastName":null},"age":32}
If I change the type in UserInfo class to string like,
@Document(collection = "userinfo")
public class UserInfo {
@Id
private String id;
private String name;
private int age;
}
The response changes to
{"id":"5c62588e5e1fbc37dc9746d3","name":"{ \"first\" : \"rajan\", \"last\" : \"rawat\" }","age":32}
which basically gives a string representation of the object from collection.
My Questions.
- Is there something wrong with the way I designed the collection in mongoDB. I am assuming my use case is a reason why the Object type would have been introduced.
- How do I map this collection in java i.e @Document. What am I missing ? Is there Something else I need to configure in the Class Name
@Documentto Name class and add@DBRefto the field in the UserInfo