Native MongoDB Java Driver examples demonstrating core capabilities through the TechMart e-commerce story.
This module uses the MongoDB Java Sync Driver directly, giving you full control over how you interact with MongoDB. Perfect for:
- Understanding MongoDB fundamentals
- Performance-critical applications
- When you need maximum flexibility
- Learning before abstracting with frameworks
| # | Example | Key Concepts |
|---|---|---|
| Core MongoDB | ||
| E01 | Document Model | Rich documents, nested structures, arrays |
| E02 | Flexible Schema | Schema evolution, polymorphic documents |
| E03 | CRUD Operations | Insert, find, update, delete patterns |
| E04 | Index Types | Single, compound, text, geo, TTL indexes |
| E05 | Aggregation Framework | Pipelines, $group, $lookup, $facet |
| E06 | Transactions | Multi-document ACID transactions |
| E07 | Change Streams | Real-time data notifications |
| E08 | Materialized Views | $merge, incremental updates |
| E09 | Schema Validation | JSON Schema enforcement |
| Time Series | ||
| E10 | Time Series Collections | IoT data, TTL, granularity |
| E11 | Window Functions | Moving averages, ranking |
| Security | ||
| E12 | Queryable Encryption | Client-side encryption concepts |
| E13 | Client-Side FLE | Crypto-shredding for GDPR |
| Atlas Features | ||
| E14 | Atlas Search | Full-text, fuzzy, autocomplete |
| E15 | Vector Search | Embeddings, semantic search, RAG |
| Scaling | ||
| E16 | Replica Sets | Read preferences, write concerns |
| E17 | Sharding | Shard keys, distribution |
# From this directory
mvn compile
# 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"
# List all examples
mvn exec:java -Dexec.mainClass="com.mongodb.examples.TechMartRunner" -Dexec.args="list"export MONGODB_URI="mongodb://localhost:27017"
export MONGODB_DATABASE="techmart"mongodb-driver-examples/
├── pom.xml
└── src/main/java/com/mongodb/examples/
├── common/
│ ├── MongoClientManager.java
│ └── SampleDataLoader.java
├── core/
│ ├── E01_DocumentModel.java
│ ├── E02_FlexibleSchema.java
│ └── ...
├── timeseries/
├── security/
├── atlas/
└── scaling/
// Connection
MongoClient client = MongoClients.create(connectionString);
MongoDatabase db = client.getDatabase("techmart");
MongoCollection<Document> coll = db.getCollection("products");
// CRUD
coll.insertOne(document);
coll.find(Filters.eq("category", "Electronics"));
coll.updateOne(filter, Updates.set("price", 999));
coll.deleteOne(filter);
// Aggregation
coll.aggregate(Arrays.asList(
Aggregates.match(filter),
Aggregates.group("$category", Accumulators.sum("total", "$amount")),
Aggregates.sort(Sorts.descending("total"))
));
// Transactions
try (ClientSession session = client.startSession()) {
session.startTransaction();
collection1.updateOne(session, filter, update);
collection2.insertOne(session, document);
session.commitTransaction();
}