Skip to content

schabiyo-eng/mongodb-java-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Java Examples - TechMart

A comprehensive collection of Java examples demonstrating MongoDB's capabilities through TechMart, a fictional global e-commerce platform.

🏗️ Project Structure

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/...

🎯 Choose Your Approach

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

📖 The TechMart Story

Both modules follow the same compelling narrative - building TechMart from startup to global scale:

  1. Launch - Flexible product catalog with the document model
  2. Scale - Millions of orders with transactions
  3. React - Real-time inventory alerts with change streams
  4. Analyze - Business intelligence with aggregations
  5. Secure - Customer PII protection with encryption
  6. Search - AI-powered semantic search
  7. Expand - Global scaling with sharding

🚀 Quick Start

Prerequisites

  • Java 17+
  • Maven 3.6+
  • MongoDB 7.0+ (local) or MongoDB Atlas

Build All Modules

# Clone and build
git clone https://github.com/your-org/mongodb-java-examples.git
cd mongodb-java-examples
mvn clean compile

Run Native Driver Examples

cd 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"

Run Spring Boot Examples

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"

🔧 Configuration

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"

📋 Examples Overview

Native Driver Module (17 examples)

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

Spring Boot Module (17 examples)

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

🔄 Side-by-Side Comparison

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)

📚 Additional Resources

📄 License

Apache License 2.0


Built with ❤️ for the MongoDB Developer Community

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages