Use Cases

Claude Code के साथ NoSQL/MongoDB Practical Guide

Claude Code का उपयोग करके NoSQL/MongoDB development सीखें। Practical tips और code examples शामिल हैं।

MongoDB Development को Claude Code के साथ Efficient बनाएं

MongoDB एक document-oriented NoSQL database है, जिसकी flexible schema design इसकी विशेषता है। Claude Code का उपयोग करके, appropriate schema design और aggregation pipeline build efficiently आगे बढ़ाया जा सकता है।

Schema Design

Embed vs Reference का निर्णय

> E-commerce site के लिए schema design करो।
> user, product, order — तीन collections।
> Performance को ध्यान में रखते हुए design बनाओ।
// Mongoose schema definition
import { Schema, model } from 'mongoose';

// Order schema: product information embedded (denormalized)
const orderSchema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: 'User', required: true },
  items: [{
    productId: { type: Schema.Types.ObjectId, ref: 'Product' },
    name: String,        // Denormalized: order के समय का product name रखें
    price: Number,       // Denormalized: order के समय की price रखें
    quantity: { type: Number, min: 1 },
  }],
  totalAmount: { type: Number, required: true },
  status: {
    type: String,
    enum: ['pending', 'confirmed', 'shipped', 'delivered', 'cancelled'],
    default: 'pending',
  },
  shippingAddress: {
    postalCode: String,
    state: String,
    city: String,
    line1: String,
    line2: String,
  },
}, { timestamps: true });

// Index configuration
orderSchema.index({ userId: 1, createdAt: -1 });
orderSchema.index({ status: 1, createdAt: -1 });

export const Order = model('Order', orderSchema);

Claude Code “ऐसा data जो अक्सर एक साथ पढ़ा जाता है, उसे embed करो; ऐसा data जो independently update होता है, उसे reference रखो” — इस design principle के आधार पर सुझाव देता है।

Aggregation Pipeline

Sales Analysis Query

> Month-wise और category-wise sales के लिए aggregation pipeline बनाओ।
const salesReport = await Order.aggregate([
  {
    $match: {
      status: { $in: ['confirmed', 'shipped', 'delivered'] },
      createdAt: {
        $gte: new Date('2026-01-01'),
        $lt: new Date('2026-04-01'),
      },
    },
  },
  { $unwind: '$items' },
  {
    $lookup: {
      from: 'products',
      localField: 'items.productId',
      foreignField: '_id',
      as: 'product',
    },
  },
  { $unwind: '$product' },
  {
    $group: {
      _id: {
        month: { $month: '$createdAt' },
        category: '$product.category',
      },
      totalRevenue: { $sum: { $multiply: ['$items.price', '$items.quantity'] } },
      orderCount: { $sum: 1 },
    },
  },
  { $sort: { '_id.month': 1, totalRevenue: -1 } },
]);

Index Optimization

> इस collection के search patterns के लिए optimal indexes suggest करो।

Claude Code query patterns को analyze करके compound indexes और text indexes के design suggest करता है। explain() का result दिखाने पर, वह index सही तरीके से काम कर रहा है या नहीं — यह भी confirm करने में help करता है।

Validation और Middleware

Mongoose के middleware (pre/post hook) और custom validation implement करना भी Claude Code के forte में आता है।

orderSchema.pre('save', function(next) {
  this.totalAmount = this.items.reduce(
    (sum, item) => sum + item.price * item.quantity, 0
  );
  next();
});

Summary

Claude Code का उपयोग करके, MongoDB के schema design से लेकर aggregation pipeline build और performance optimization तक, सब कुछ efficiently आगे बढ़ाया जा सकता है। SQL query optimization और database migration को भी reference के लिए देखें।

MongoDB के details के लिए MongoDB की official documentation देखें।

#Claude Code #MongoDB #NoSQL #database #backend

अपने Claude Code वर्कफ़्लो को अगले स्तर पर ले जाएँ

Claude Code में तुरंत कॉपी-पेस्ट करने योग्य 50 आज़माए हुए प्रॉम्प्ट टेम्पलेट।

मुफ़्त

मुफ़्त PDF: 5 मिनट में Claude Code चीटशीट

मुख्य कमांड, शॉर्टकट और प्रॉम्प्ट उदाहरण एक प्रिंट योग्य पृष्ठ पर।

PDF डाउनलोड करें
M

लेखक के बारे में

Masa

Claude Code का गहराई से उपयोग करने वाले इंजीनियर। claudecode-lab.com चलाते हैं, जो 10 भाषाओं में 2,000 से अधिक पेजों वाला टेक मीडिया है।