The
User and
Address data is a
one-to-many relationship. A user can have, in general, 1 to 3 ( a small number) addresses. In MongoDB, this is modeled by
embedding the address data into the user data.
It is unusual, to create two collections one for each of the user and the address - especially when the addresses are going to be a very small number, and the kind of data being stored. The document structure would be something like this in a "user" collection:
Note the
address is an
array type field, with multiple address elements.
The two
Java POJOs are the
User and the
Address. The
User class will have a property called as
address of type
List<Address>. Using Spring-Data APIs for MongoDB, the CRUD operations can be performed on the "user" collection. For example, using
MongoTemplate class:
References:
Data Model DesignMongoTemplate
NOTE: One can also consider using
MongoDB Java Driver APIs for the same application.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer