Elysia Tools

Browse tools, examples, visual explainers, and topic guides from one clear workspace.

Top Categories

Hubs

Language
ZHESPTRUFRDEEN

Elysia Tools

Navigation

Database

MongoDB Samples

Comprehensive MongoDB NoSQL database examples including CRUD operations, aggregation framework, indexing, and scaling strategies

Samples

Entries inside this sample collection

CodeSimple

MongoDB CRUD Operations

Essential MongoDB Create, Read, Update, and Delete operations with proper error handling

Difficulty

3/10

Estimated time

20 min

Tags

mongodb, crud, database

Prerequisites

MongoDB basics, JavaScript/TypeScript, Database concepts

plaintext
// MongoDB CRUD Operations
const { MongoClient, ObjectId } = require('mongodb');

class DatabaseService {
    constructor(uri = 'mongodb://localhost:27017') {
        this.client = new MongoClient(uri);
        this.dbName = 'myapp';
    }

    async connect() {
        await this.client.connect();
        this.db = this.client.db(this.dbName);
    }

    async disconnect() {
        await this.client.close();
    }

    // CREATE operations
    async insertOne(collection, document) {
        const result = await this.db.collection(collection).insertOne(document);
        return result.insertedId;
    }

    async insertMany(collection, documents) {
        const result = await this.db.collection(collection).insertMany(documents);
        return result.insertedIds;
    }

    // READ operations
    async findOne(collection, query = {}) {
        return await this.db.collection(collection).findOne(query);
    }

    async find(collection, query = {}, options = {}) {
        const cursor = this.db.collection(collection).find(query, options);
        return await cursor.toArray();
    }

    async findById(collection, id) {
        return await this.db.collection(collection).findOne({
            _id: new ObjectId(id)
        });
    }

    // UPDATE operations
    async updateOne(collection, query, update) {
        return await this.db.collection(collection).updateOne(query, update);
    }

    async updateMany(collection, query, update) {
        return await this.db.collection(collection).updateMany(query, update);
    }

    async replaceOne(collection, query, replacement) {
        return await this.db.collection(collection).replaceOne(query, replacement);
    }

    // DELETE operations
    async deleteOne(collection, query) {
        return await this.db.collection(collection).deleteOne(query);
    }

    async deleteMany(collection, query) {
        return await this.db.collection(collection).deleteMany(query);
    }

    // Aggregation
    async aggregate(collection, pipeline) {
        const cursor = this.db.collection(collection).aggregate(pipeline);
        return await cursor.toArray();
    }
}

// Example usage
async function example() {
    const db = new DatabaseService();
    await db.connect();

    // Insert a document
    const userId = await db.insertOne('users', {
        name: 'John Doe',
        email: '[email protected]',
        age: 30,
        createdAt: new Date()
    });

    // Find user
    const user = await db.findById('users', userId);

    // Update user
    await db.updateOne('users',
        { _id: userId },
        { $set: { age: 31 } }
    );

    await db.disconnect();
}
CodeIntermediate

MongoDB Aggregation Framework

Advanced data processing using MongoDB aggregation pipeline for complex analytics

Difficulty

7/10

Estimated time

30 min

Tags

mongodb, aggregation, analytics

Prerequisites

MongoDB basics, Aggregation concepts, JavaScript

plaintext
// MongoDB Aggregation Framework
class AnalyticsService {
    constructor(db) {
        this.db = db;
    }

    // Sales analytics example
    async getSalesAnalytics(startDate, endDate) {
        const pipeline = [
            {
                $match: {
                    createdAt: {
                        $gte: startDate,
                        $lte: endDate
                    }
                }
            },
            {
                $group: {
                    _id: {
                        year: { $year: '$createdAt' },
                        month: { $month: '$createdAt' },
                        day: { $dayOfMonth: '$createdAt' }
                    },
                    totalSales: { $sum: '$amount' },
                    orderCount: { $sum: 1 },
                    averageOrderValue: { $avg: '$amount' }
                }
            },
            {
                $sort: { '_id.year': 1, '_id.month': 1, '_id.day': 1 }
            }
        ];

        return await this.db.collection('orders').aggregate(pipeline).toArray();
    }

    // Customer segmentation
    async getCustomerSegments() {
        const pipeline = [
            {
                $lookup: {
                    from: 'orders',
                    localField: '_id',
                    foreignField: 'customerId',
                    as: 'orders'
                }
            },
            {
                $addFields: {
                    totalSpent: { $sum: '$orders.amount' },
                    orderCount: { $size: '$orders' },
                    lastOrderDate: { $max: '$orders.createdAt' }
                }
            },
            {
                $addFields: {
                    segment: {
                        $switch: {
                            branches: [
                                {
                                    case: { $gte: ['$totalSpent', 1000] },
                                    then: 'VIP'
                                },
                                {
                                    case: { $gte: ['$totalSpent', 500] },
                                    then: 'Premium'
                                },
                                {
                                    case: { $gte: ['$totalSpent', 100] },
                                    then: 'Regular'
                                }
                            ],
                            default: 'Basic'
                        }
                    }
                }
            },
            {
                $group: {
                    _id: '$segment',
                    customers: { $sum: 1 },
                    totalRevenue: { $sum: '$totalSpent' },
                    avgCustomerValue: { $avg: '$totalSpent' }
                }
            }
        ];

        return await this.db.collection('customers').aggregate(pipeline).toArray();
    }

    // Product performance analytics
    async getProductPerformance() {
        const pipeline = [
            { $unwind: '$items' },
            {
                $group: {
                    _id: '$items.productId',
                    totalSold: { $sum: '$items.quantity' },
                    revenue: { $sum: { $multiply: ['$items.quantity', '$items.price'] } },
                    orderCount: { $sum: 1 }
                }
            },
            {
                $lookup: {
                    from: 'products',
                    localField: '_id',
                    foreignField: '_id',
                    as: 'product'
                }
            },
            { $unwind: '$product' },
            {
                $addFields: {
                    profitMargin: {
                        $multiply: [
                            { $divide: [{ $subtract: ['$revenue', '$product.cost'] }, '$revenue'] },
                            100
                        ]
                    }
                }
            },
            {
                $sort: { revenue: -1 }
            }
        ];

        return await this.db.collection('orders').aggregate(pipeline).toArray();
    }

    // Time series analysis
    async getTimeSeriesAnalysis(collection, dateField, interval = 'day') {
        const dateExpression = '$' + dateField;
        const intervalFormats = {
            day: { $dateToString: { format: '%Y-%m-%d', date: dateExpression } },
            month: { $dateToString: { format: '%Y-%m', date: dateExpression } },
            year: { $dateToString: { format: '%Y', date: dateExpression } }
        };

        const pipeline = [
            {
                $group: {
                    _id: intervalFormats[interval],
                    count: { $sum: 1 },
                    avgValue: { $avg: '$value' },
                    totalValue: { $sum: '$value' }
                }
            },
            { $sort: { _id: 1 } }
        ];

        return await this.db.collection(collection).aggregate(pipeline).toArray();
    }
}
CodeIntermediate

MongoDB Indexing Strategies

Optimize query performance with proper indexing strategies including compound, text, and geospatial indexes

Difficulty

6/10

Estimated time

25 min

Tags

mongodb, indexing, performance

Prerequisites

MongoDB basics, Performance optimization, Database indexing

plaintext
// MongoDB Indexing Strategies
class IndexingService {
    constructor(db) {
        this.db = db;
    }

    async createIndexes() {
        const collections = ['users', 'products', 'orders', 'reviews'];

        // Single field indexes
        await this.db.collection('users').createIndex({ email: 1 }, { unique: true });
        await this.db.collection('users').createIndex({ username: 1 }, { unique: true });

        // Compound indexes
        await this.db.collection('products').createIndex(
            { category: 1, price: 1, rating: -1 }
        );
        await this.db.collection('orders').createIndex(
            { customerId: 1, createdAt: -1 }
        );

        // Text indexes for search functionality
        await this.db.collection('products').createIndex(
            { name: 'text', description: 'text' },
            {
                weights: { name: 10, description: 1 },
                name: 'product_search_index'
            }
        );

        // Geospatial indexes
        await this.db.collection('users').createIndex(
            { location: '2dsphere' }
        );

        // TTL (Time To Live) indexes for automatic data expiration
        await this.db.collection('sessions').createIndex(
            { expiresAt: 1 },
            { expireAfterSeconds: 0 }
        );

        // Partial indexes (only index documents that meet criteria)
        await this.db.collection('users').createIndex(
            { emailVerificationToken: 1 },
            {
                partialFilterExpression: {
                    emailVerificationToken: { $exists: true }
                }
            }
        );

        // Sparse indexes (only index documents with the field)
        await this.db.collection('reviews').createIndex(
            { helpfulVotes: 1 },
            { sparse: true }
        );
    }

    async analyzeIndexUsage(collectionName) {
        const stats = await this.db.collection(collectionName).aggregate([
            { $indexStats: {} },
            {
                $project: {
                    name: 1,
                    usageCount: '$ops.accesses.ops',
                    lastUsed: '$ops.accesses.last'
                }
            }
        ]).toArray();

        return stats;
    }

    async explainQuery(collectionName, query) {
        return await this.db.collection(collectionName)
            .find(query)
            .explain('executionStats');
    }

    async createOptimizedIndexes() {
        // Covering index example (includes all fields needed for query)
        await this.db.collection('products').createIndex(
            { category: 1, price: 1 },
            {
                name: 'category_price_covering',
                background: true
            }
        );

        // Index with collation for case-insensitive search
        await this.db.collection('users').createIndex(
            { name: 1 },
            {
                collation: { locale: 'en', strength: 2 },
                name: 'name_case_insensitive'
            }
        );

        // Wildcard index (indexes all fields except _id)
        await this.db.collection('logs').createIndex(
            { '$**': 1 },
            { name: 'all_fields_wildcard' }
        );
    }

    async dropUnusedIndexes() {
        const collections = await this.db.listCollections().toArray();

        for (const collection of collections) {
            const indexes = await this.db.collection(collection.name)
                .listIndexes()
                .toArray();

            const indexStats = await this.analyzeIndexUsage(collection.name);
            const usedIndexes = new Set(indexStats.map(stat => stat.name));

            for (const index of indexes) {
                if (index.name !== '_id_' && !usedIndexes.has(index.name)) {
                    console.log('Dropping unused index: ' + index.name);
                    await this.db.collection(collection.name).dropIndex(index.name);
                }
            }
        }
    }
}
CodeIntermediate

MongoDB Schema Design Patterns

Best practices for MongoDB schema design including embedding, referencing, and hybrid approaches

Difficulty

7/10

Estimated time

35 min

Tags

mongodb, schema, design

Prerequisites

MongoDB basics, Database design, Data modeling

plaintext
// MongoDB Schema Design Patterns

// 1. Embedding Pattern - One-to-One Relationship
const userSchema = {
    _id: ObjectId,
    name: String,
    email: String,
    profile: {
        firstName: String,
        lastName: String,
        avatar: String,
        bio: String,
        preferences: {
            theme: String,
            notifications: Boolean,
            language: String
        }
    },
    createdAt: Date,
    updatedAt: Date
};

// 2. Embedding Pattern - One-to-Few Relationship
const productSchema = {
    _id: ObjectId,
    name: String,
    description: String,
    price: Number,
    category: String,
    images: [
        {
            url: String,
            alt: String,
            isPrimary: Boolean
        }
    ],
    tags: [String],
    variants: [
        {
            name: String,
            sku: String,
            price: Number,
            inventory: Number
        }
    ],
    createdAt: Date
};

// 3. Referencing Pattern - One-to-Many Relationship
const orderSchema = {
    _id: ObjectId,
    customerId: ObjectId, // Reference to users collection
    orderNumber: String,
    status: String,
    items: [
        {
            productId: ObjectId, // Reference to products collection
            quantity: Number,
            price: Number,
            customizations: Object
        }
    ],
    shippingAddress: {
        street: String,
        city: String,
        state: String,
        zipCode: String,
        country: String
    },
    totalAmount: Number,
    createdAt: Date
};

// 4. Extended Reference Pattern
const userOrderSchema = {
    _id: ObjectId,
    customerId: ObjectId,
    orderNumber: String,
    status: String,
    items: [
        {
            productId: ObjectId,
            quantity: Number,
            price: Number,
            // Embedded frequently accessed product data
            productInfo: {
                name: String,
                imageUrl: String,
                category: String
            }
        }
    ],
    totalAmount: Number,
    createdAt: Date
};

// 5. Bucket Pattern - Time Series Data
const sensorDataSchema = {
    _id: ObjectId,
    sensorId: ObjectId,
    startDate: Date,
    endDate: Date,
    count: Number,
    measurements: {
        min: Number,
        max: Number,
        sum: Number,
        avg: Number
    },
    data: [
        {
            timestamp: Date,
            value: Number,
            quality: String
        }
    ]
};

// 6. Computed Pattern
const productStatsSchema = {
    _id: ObjectId,
    productId: ObjectId,
    totalViews: Number,
    totalPurchases: Number,
    averageRating: Number,
    reviewCount: Number,
    conversionRate: Number,
    lastUpdated: Date
};

// 7. Subset Pattern - Frequently accessed data subset
const userDashboardSchema = {
    _id: ObjectId,
    userId: ObjectId,
    recentOrders: [
        {
            orderId: ObjectId,
            orderNumber: String,
            totalAmount: Number,
            status: String,
            createdAt: Date
        }
    ],
    favoriteProducts: [
        {
            productId: ObjectId,
            name: String,
            imageUrl: String,
            price: Number
        }
    ],
    notifications: [
        {
            type: String,
            message: String,
            isRead: Boolean,
            createdAt: Date
        }
    ],
    lastUpdated: Date
};

// Schema validation rules
const createValidationRules = () => {
    return {
        $jsonSchema: {
            bsonType: 'object',
            required: ['name', 'email', 'createdAt'],
            properties: {
                name: {
                    bsonType: 'string',
                    description: 'must be a string and is required',
                    minLength: 2,
                    maxLength: 50
                },
                email: {
                    bsonType: 'string',
                    pattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
                    description: 'must be a valid email address'
                },
                age: {
                    bsonType: 'int',
                    minimum: 0,
                    maximum: 150,
                    description: 'must be an integer in [0, 150]'
                },
                status: {
                    enum: ['active', 'inactive', 'suspended'],
                    description: 'can only be one of the enum values'
                }
            }
        }
    };
};
ConfigComplex

MongoDB Scaling Strategies

Implement horizontal scaling with sharding and vertical scaling optimization techniques

Difficulty

9/10

Estimated time

45 min

Tags

mongodb, scaling, sharding

Prerequisites

MongoDB advanced, Docker, Cluster management, System administration

config
# MongoDB Sharded Cluster Configuration

version: '3.8'

services:
  # Config servers (replica set)
  config-server-1:
    image: mongo:6.0
    command: mongod --configsvr --replSet config-replica-set --bind_ip_all
    ports:
      - "27019:27019"
    volumes:
      - config-server-1-data:/data/db

  config-server-2:
    image: mongo:6.0
    command: mongod --configsvr --replSet config-replica-set --bind_ip_all
    ports:
      - "27020:27019"
    volumes:
      - config-server-2-data:/data/db

  config-server-3:
    image: mongo:6.0
    command: mongod --configsvr --replSet config-replica-set --bind_ip_all
    ports:
      - "27021:27019"
    volumes:
      - config-server-3-data:/data/db

  # Shard servers (replica sets)
  shard1-server-1:
    image: mongo:6.0
    command: mongod --shardsvr --replSet shard1-replica-set --bind_ip_all
    ports:
      - "27022:27017"
    volumes:
      - shard1-server-1-data:/data/db

  shard1-server-2:
    image: mongo:6.0
    command: mongod --shardsvr --replSet shard1-replica-set --bind_ip_all
    ports:
      - "27023:27017"
    volumes:
      - shard1-server-2-data:/data/db

  shard2-server-1:
    image: mongo:6.0
    command: mongod --shardsvr --replSet shard2-replica-set --bind_ip_all
    ports:
      - "27024:27017"
    volumes:
      - shard2-server-1-data:/data/db

  shard2-server-2:
    image: mongo:6.0
    command: mongod --shardsvr --replSet shard2-replica-set --bind_ip_all
    ports:
      - "27025:27017"
    volumes:
      - shard2-server-2-data:/data/db

  # Mongos router
  mongos:
    image: mongo:6.0
    command: mongos --configdb config-replica-set/config-server-1:27019,config-server-2:27019,config-server-3:27019 --bind_ip_all --port 27017
    ports:
      - "27017:27017"
    depends_on:
      - config-server-1
      - config-server-2
      - config-server-3
      - shard1-server-1
      - shard1-server-2
      - shard2-server-1
      - shard2-server-2

  # Cluster initialization
  cluster-setup:
    image: mongo:6.0
    depends_on:
      - mongos
    command: |
      sh -c "
      sleep 30

      # Initialize config server replica set
      mongo --host config-server-1:27019 --eval '
      rs.initiate({
        _id: "config-replica-set",
        configsvr: true,
        members: [
          { _id: 0, host: "config-server-1:27019" },
          { _id: 1, host: "config-server-2:27019" },
          { _id: 2, host: "config-server-3:27019" }
        ]
      })'

      sleep 10

      # Initialize shard1 replica set
      mongo --host shard1-server-1:27022 --eval '
      rs.initiate({
        _id: "shard1-replica-set",
        members: [
          { _id: 0, host: "shard1-server-1:27022" },
          { _id: 1, host: "shard1-server-2:27023" }
        ]
      })'

      # Initialize shard2 replica set
      mongo --host shard2-server-1:27024 --eval '
      rs.initiate({
        _id: "shard2-replica-set",
        members: [
          { _id: 0, host: "shard2-server-1:27024" },
          { _id: 1, host: "shard2-server-2:27025" }
        ]
      })'

      sleep 20

      # Add shards to cluster
      mongo --host mongos:27017 --eval '
      sh.addShard("shard1-replica-set/shard1-server-1:27022,shard1-server-2:27023")
      sh.addShard("shard2-replica-set/shard2-server-1:27024,shard2-server-2:27025")

      # Enable sharding for database
      sh.enableSharding("myapp")

      # Shard collections
      sh.shardCollection("myapp.users", { "_id": "hashed" })
      sh.shardCollection("myapp.orders", { "customerId": 1, "createdAt": 1 })
      sh.shardCollection("myapp.products", { "category": 1 })
      '"

volumes:
  config-server-1-data:
  config-server-2-data:
  config-server-3-data:
  shard1-server-1-data:
  shard1-server-2-data:
  shard2-server-1-data:
  shard2-server-2-data:

Tools

Tools frequently paired with this sample

Related

Related sample collections

Related

Related workflows

Hub

Infer JSON Schema from Samples and Spreadsheets

Derive a reviewable JSON Schema draft from representative JSON samples or XLSX rows, then inspect types, requiredness, nulls, and outliers before treating it as a contract.

Hub

Database Schema Migration

Plan a safe database migration by normalizing source data, reviewing schema changes, validating relationships, and preparing rollback assets.

Hub

Repair a Rejected CSV Import and Load It into Your Database

Diagnose why a CSV import keeps failing, map messy headers onto the target schema, remove duplicates under a stated policy, and emit typed INSERT statements or a bulk-load TSV.

Hub

XLSX ETL Ingestion and Warehouse Handoff

Turn mixed workbook and CSV sources into traceable, schema-aware outputs for SQL loading, Parquet pipelines, and object-storage batch handoff.

Home
Tools3206
Samples593
Visualizations468
Viewers103
Hubs236
Tags333
Images, Audio & Video657

Image, audio, and video processing, conversion, and optimization tools

Math, Date & Finance645

Calculators, numerics, date logic, statistics, and finance tools

Design & Color298

Color, layout, graphics, visual styling, and design helper tools

Text & Writing209

Writing, cleanup, formatting, extraction, and text analysis tools

Developer & Web193

Developer utilities, networking, web debugging, and automation helpers

Conversion & Encoding175

Format, file, archive, unit, and encoding conversion tools

Article to X and LinkedIn Drafts: Fact Review and Handoff
Markdown Interactive Deck Review and Offline Handoff
Small-Room Furniture Layout Review and Move-In Handoff
FLAC/WAV Source Screening and Evidence Handoff
Self-Hosted Giveaway Widget Review and Embed Handoff

© 2026 Elysia Tools. All rights reserved.

DocsPricingAboutTerms of ServicePrivacy PolicyCookiesContact

Development

SQL Explain Plan Visualizer

Parse EXPLAIN / EXPLAIN ANALYZE output (PostgreSQL/MySQL/SQLite) into a cost tree, flag estimated-vs-actual row divergence, and suggest indexes

Security

SQL Injection Detector

Detect common SQL injection attack patterns in input strings

Development

Database Backup Generator

Generate SQL backup scripts for database tables

Development

SQL Performance Analyzer

Analyze SQL queries for potential performance issues

Format Conversion

CSV Flavor to SQL INSERT Emitter

Parse CSV/Excel-CSV/TSV (auto dialect, RFC 4180) into MySQL/PostgreSQL/SQLite/SQL Server INSERT batches with type inference and ON CONFLICT/IGNORE

Data Processing

CSV to Database Migration Planner

Infer a relational schema from CSV data and generate create-table plus ALTER migration plans for PostgreSQL, MySQL, SQLite, or SQL Server

Development

Database Migration Generator

Generate SQL migration scripts for database schema changes

Development

Database Schema Diff

Compare two database schemas and generate migration-oriented change reports

Database

PostgreSQL Advanced Samples

Advanced PostgreSQL database examples including optimization strategies, complex queries, indexing, and performance tuning

Database

Redis Samples

Comprehensive Redis cache solution examples including basic operations, pub/sub patterns, clustering, and performance optimization

Python

Web Database Python Samples

Web Python database examples including SQLite connection, SQL queries, and transaction handling

Database

Sequelize Samples

Sequelize Node.js ORM with model definitions, associations, migrations, queries, and database operations

Security

SQL Injection Samples

Educational collection of SQL injection payloads for security testing, vulnerability assessment, and defensive coding practices

Data Formats

SQL Scripts Samples

SQL query examples and database scripts for various scenarios and use cases (primarily MySQL syntax)