SPRING CONCEPTS

MongoDB essentials

Spring Concepts / Detailed guide

MongoDB stores BSON documents in collections. A database groups collections. A todo might contain _id, title, and completed. BSON supports more types than JSON; APIs expose JSON views of the stored data. A Java document is mapped by Spring Data rather than manually converted in each controller.

@Document names the collection. @Id selects the id field. MongoRepository<Todo,String> identifies the document and id types. Derived query names such as findByIdAndOwnerId instruct Spring Data to build an equality query. MongoDB is not accessed using @Entity, JpaRepository, or SQL joins in these projects.

Atlas is a managed MongoDB hosting service. Its website account manages projects and clusters; a separate database user authenticates application connections. Your app also needs a matching network IP rule. Each application uses its own database name to keep the labs independent.

A single document write is atomic. The read-then-save update shown in these labs can overwrite concurrent changes. That tradeoff keeps the first implementation teachable. A later exercise can add optimistic locking with @Version and return a conflict for a stale update.

← All Spring Concepts · Learning path

Java 21 · Spring Boot 4.1.1 · Edupoly