Announcement Announcement Module
No announcement yet.
[Help]MongoDB: Using mongotemplate for dynamic fields Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Help]MongoDB: Using mongotemplate for dynamic fields

    Hi, I'm currently working on enhancement for an existing java code, and I really need help getting understanding how the mongotemplate.find() function converts dynamic JSON objects.

    So for example, if my table named 'subscribers' in the mongo database looks like this:
    /* 0 */
    "username": sskk,
    "destination": {Exchange: "asdf", Routingkey: "ffff"},             
    destinationtypeID: 1
    /* 1 */
    "destination":{Email: ""},
    destinationtypeID: 2
    then I want use mongotemplate.find(query,Subscriber.class) to load this table into a list of Subscribers which is a class I want to create in Java. I wonder how do I declare the destination field in the subscriber class, and how do I retrieve the values in there?

    right now the mongo database I'm working on does not have the fields destination and destinationtypeID, instead it only has a string field named 'email', and this is how the subscriber class look like right now:

    public class Subscriber {
    	@NotBlank(message = "Subscriber userName is required")
    	private String userName;
            private String email;
            public String getUserName() {
    		return userName;
    	public void setUserName(String userName) {
    		this.userName = userName;
    	public String getEmail() {
    		return email;
    	public void setEmail(String email) { = email;
    How should I declare the Destination inside the Subscriber class in order for mongotemplate.find() to be able to save the data into Destination no matter the destination field in the JSON object is of form
    {exchange:... RoutingKey:...} or {email:...}

    I'm new to Java and to Spring, so this may seem like a stupid question. But I was having a hard time researching how to do this on the internet. Any kind of help is appreciated, and If you are unsure of what I'm asking, please tell me and I ll try explain it better.