Saving, Updating, and Removing Documents
Explore documents and repositories through the Spring Data MongoDB framework.
Spring Data MongoDB allows saving, updating, and removing documents through its effortless integration. It offers intuitive methods like save
, findByIdAndUpdate
, and deleteById
to perform these operations on MongoDB collections with greater ease.
Document
In MongoDB, documents are the basic unit of data storage. They are JSON-like structures that store data as key-value pairs, allowing flexible and schemaless data modeling.
We’ll cover the same example of the Book
and Author
POJOs referencing the book
and author
database documents and convert them into MongoDB document classes.
Create a Book
document
Let’s learn a few handy annotations to transform a POJO into a MongoDB document.
The most common annotations are @Document
and @Id
:
-
The
@Document
annotation allows mapping a POJO to a database document by the underlying data access layer. -
The
@Id
annotation allows mapping a property to an identifier in the database document.
We can use these annotations to convert the Book
POJO from the com.smartdiscover.document
package to a ...