Working With Documents
- Define Java classes annotated with @Document to represent documents in MongoDB collections.
- The class name will be used as the collection name by default. Can specify collection name explicitly using @Document(collection=”collectionName”).
- Fields in the document are mapped to properties in the Java class. Use annotations like @Id, @Field, etc. to customize mapping.
- @Id annotation denotes the property that holds the unique identifier value of the document. It can be of any valid type like String, Integer, etc.
Spring Data MongoDB
Spring Data MongoDB is an Extremely useful tool for Java developers working with MongoDB databases. It simplifies data access and manipulation, provides a consistent programming model, and enhances developer productivity when building MongoDB-backed applications in the Spring ecosystem. It allows MongoDB collections and documents to be mapped to Java POJO classes using annotations like @Document and @Id. This object-document mapping reduces boilerplate code. It provides a MongoTemplate class for executing common MongoDB operations like find, save, update, and delete. This acts as the central API for data access.