A comprehensive collection of Java examples demonstrating MongoDB's capabilities through TechMart, a fictional global e-commerce platform.
This is a multi-module Maven project with two implementation approaches:
mongodb-java-examples/
├── pom.xml # Parent POM
├── README.md # This file
├── docs/
│
├── mongodb-driver-examples/ # Native Java Driver examples
│ ├── pom.xml
│ ├── README.md
│ └── src/main/java/...
│
└── spring-boot-examples/ # Spring Boot + Spring Data examples
├── pom.xml
├── README.md
└── src/main/java/...
| Module | Best For | Key Features |
|---|---|---|
| mongodb-driver-examples | Direct control, understanding fundamentals | Native driver API, Document class, explicit queries |
| spring-boot-examples | Rapid development, Spring ecosystem | @Document POJOs, Repository pattern, @Transactional |
Both modules follow the same compelling narrative - building TechMart from startup to global scale:
- Launch - Flexible product catalog with the document model
- Scale - Millions of orders with transactions
- React - Real-time inventory alerts with change streams
- Analyze - Business intelligence with aggregations
- Secure - Customer PII protection with encryption
- Search - AI-powered semantic search
- Expand - Global scaling with sharding
- Java 17+
- Maven 3.6+
- MongoDB 7.0+ (local) or MongoDB Atlas
# Clone and build
git clone https://github.com/your-org/mongodb-java-examples.git
cd mongodb-java-examples
mvn clean compilecd mongodb-driver-examples
# Load sample data
mvn exec:java -Dexec.mainClass="com.mongodb.examples.common.SampleDataLoader"
# Run any example
mvn exec:java -Dexec.mainClass="com.mongodb.examples.core.E01_DocumentModel"
mvn exec:java -Dexec.mainClass="com.mongodb.examples.core.E05_AggregationFramework"cd spring-boot-examples
# Show available examples menu
mvn spring-boot:run
# Run a specific example
mvn spring-boot:run -Dspring-boot.run.arguments="--example=E01"
# Run ALL examples
mvn spring-boot:run -Dspring-boot.run.arguments="--example=ALL"
# Or from parent directory
mvn spring-boot:run -pl spring-boot-examples -Dspring-boot.run.arguments="--example=E05"Set your MongoDB connection string:
# For local MongoDB
export MONGODB_URI="mongodb://localhost:27017"
# For MongoDB Atlas
export MONGODB_URI="mongodb+srv://user:pass@cluster.mongodb.net/"
# Database name (default: techmart)
export MONGODB_DATABASE="techmart"| Category | Examples |
|---|---|
| Core MongoDB | E01-E09: Document Model, Schema, CRUD, Indexes, Aggregation, Transactions, Change Streams, Materialized Views, Validation |
| Time Series | E10-E11: Time Series Collections, Window Functions |
| Security | E12-E13: Queryable Encryption, Client-Side FLE |
| Atlas Features | E14-E15: Atlas Search, Vector Search |
| Scaling | E16-E17: Replica Sets, Sharding |
| Example | Spring Data Features |
|---|---|
| E01 Document Model | @Document, @Id, @Indexed, POJO mapping |
| E02 Flexible Schema | Map<String, Object> for dynamic attributes in POJOs |
| E03 CRUD Operations | Repository methods, derived queries |
| E04 Index Types | @Indexed, @CompoundIndex, @TextIndexed, indexOps() |
| E05 Aggregation | @Aggregation, MongoTemplate, fluent API |
| E06 Transactions | @Transactional, MongoTransactionManager |
| E07 Change Streams | MongoTemplate.watch(), ChangeStreamIterable |
| E08 Materialized Views | $merge aggregation, pre-computed analytics |
| E09 Schema Validation | Bean Validation (JSR-380), @Valid, @NotBlank, @Email |
| E12 Queryable Encryption | Conceptual demo with encrypted POJOs |
| E13 Client-Side FLE | Per-customer keys, crypto-shredding for GDPR |
| E10 Time Series | IoT sensor data, time bucketing, TTL |
| E14 Atlas Search | Full-text, fuzzy matching, autocomplete |
| E15 Vector Search | AI/semantic search, RAG pattern, embeddings |
| E16 Replica Set | Read preferences, write concerns, HA |
| Feature | Native Driver | Spring Data MongoDB |
|---|---|---|
| Document creation | new Document().append(k, v) |
POJOs with @Document |
| CRUD operations | collection.insertOne(doc) |
repository.save(entity) |
| Queries | Filters.eq("field", value) |
findByField(value) |
| Aggregation | collection.aggregate(pipeline) |
@Aggregation or MongoTemplate |
| Transactions | ClientSession explicit |
@Transactional automatic |
| Indexes | collection.createIndex() |
@Indexed annotations |
| Change Streams | collection.watch() |
MongoTemplate.watch() |
| Validation | MongoDB JSON Schema | Bean Validation (JSR-380) |
Apache License 2.0
Built with ❤️ for the MongoDB Developer Community