0

I have this JSON:

{
  'name': 'Ana',
  'lastName':'Lee',
  'address': {
     'street':'321 Malvo St',
     'city':'Nowhere',
     'state':'MA',
     'zip':'010101'
   }
 }

I need to persist using MongoDB Driver in Java. How should I create field 'address' in my class?

 @Document(collection = "user")
 public class User extends AbstractEntity{

       private String name;

       private String lastName;

       private ????? address

       ...
  }

address type ?????, should be:
- a new class Address with fields street, city, etc..
- a Document
- a String
- a Map
- other

1 Answer 1

1

You need to create a new Address class,

    class Adderss {
      private String street;
      private String city;
      private String state;
      private String zip;

      //getters and setters
   }

Then you can use it in your User class,

private Address address;

Or else you can use a java.util.Map for this.

private Map<String, String> address;

Both works fine but if you need to use those address details in your logic. I recommend you to use Address class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.