Skip to main content

MongoDB

Key Features of MongoDB

FeatureDescription
Document-OrientedStores data in flexible, JSON-like documents (BSON format).
Schema-lessCollections do not enforce a fixed schema.
Horizontal ScalingNative sharding support for massive scalability.
Rich Query LanguageSupports nested queries, aggregation pipeline, and text search.
IndexingCompound, geospatial, text, hashed, and wildcard indexing.
Replication & HAReplica sets provide automatic failover and redundancy.
Aggregation FrameworkAdvanced data processing pipeline for transformation and analytics.
Change StreamsReal-time data change tracking.
Atlas IntegrationManaged MongoDB on the cloud with backups, monitoring, and scaling.

Connecting with Frameworks & ORMs

NestJS (with Mongoose)

npm install @nestjs/mongoose mongoose
app.module.ts
MongooseModule.forRoot("mongodb://localhost:27017/your_db");
user.schema.ts
@Schema()
export class User extends Document {
@Prop({ required: true })
name: string;

@Prop({ unique: true })
email: string;
}
user.module.ts
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]);

Spring Boot (with Spring Data MongoDB)

pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
application.yml
spring:
data:
mongodb:
uri: mongodb://localhost:27017/your_db
@Document("users")
public class User {
@Id
private String id;
private String name;
private String email;
}
public interface UserRepository extends MongoRepository<User, String> {}

Laravel (with Jenssegers/MongoDB)

composer require jenssegers/mongodb
config/database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
use Jenssegers\Mongodb\Eloquent\Model;

class User extends Model {
protected $connection = 'mongodb';
protected $collection = 'users';
}

Managing MongoDB with Docker

docker-compose.yml
version: "3.8"
services:
mongodb:
image: mongodb/mongodb-community-server:latest
container_name: mongo-db
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
environment:
MONGO_INITDB_DATABASE: your_db
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password

volumes:
mongo_data:
# Connect mongodb with mongosh commands
mongosh "mongodb://root:example@localhost:27017"

💡 Use mongo-express, Compass, or NoSQLBooster for GUI access.

Deploying MongoDB with Kubernetes

Basic StatefulSet + PVC:

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongo
spec:
selector:
matchLabels:
app: mongo
serviceName: "mongo"
replicas: 1
template:
metadata:
labels:
app: mongo
spec:
containers:
- name: mongo
image: mongo:6
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 5Gi

⚠️ For production, add auth, secret mounting, readiness probes, and replicas with MongoDB Helm Chart.

Best Practices

Schema Design

  • Favor embedding for fast reads (1:1 or 1:N), use referencing for N:M relationships.
  • Keep documents < 16MB. Avoid overly nested structures.

Indexing & Performance

  • Always index fields used in find, sort, and join ($lookup).
  • Monitor with explain() and optimize aggregation stages.

Security

  • Enable access control & authentication (SCRAM).
  • Use TLS and firewall rules for network-level security.

Monitoring & Backup

  • Use MongoDB Atlas, Prometheus exporters, or Ops Manager.
  • Schedule backups using mongodump, mongorestore, or volume snapshots.

Development

  • Use Mongoose validation or JSON Schema.
  • Leverage change streams for real-time use cases.
  • Use MongoDB transactions (4.0+) for atomicity across collections.

References