Use Cases

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

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

MongoDBdevelopmentको Claude Code से Efficient बनाएं

MongoDBはドキュメント指向のNoSQLdatabaseで、flexibleなスkeyマ設計が特徴 है।Claude Code का उपयोग करके、appropriateなスkeyマ設計やAggregation Pipelineのbuildを効率よく進められ है।

スkeyマ設計

埋め込み vs 参照の判断

> ECサイトのスkeyマを設計して。
> user、商品、注文の3コレクション。
> performanceを考慮した設計で。
// Mongooseスkeyマ定義
import { Schema, model } from 'mongoose';

// 注文スkeyマ:商品情報を埋め込み(非正規化)
const orderSchema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: 'User', required: true },
  items: [{
    productId: { type: Schema.Types.ObjectId, ref: 'Product' },
    name: String,        // 非正規化:注文時点の商品名を保持
    price: Number,       // 非正規化:注文時点の価格を保持
    quantity: { type: Number, min: 1 },
  }],
  totalAmount: { type: Number, required: true },
  status: {
    type: String,
    enum: ['pending', 'confirmed', 'shipped', 'delivered', 'cancelled'],
    default: 'pending',
  },
  shippingAddress: {
    postalCode: String,
    prefecture: String,
    city: String,
    line1: String,
    line2: String,
  },
}, { timestamps: true });

// インデックスsettings
orderSchema.index({ userId: 1, createdAt: -1 });
orderSchema.index({ status: 1, createdAt: -1 });

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

Claude Codeは「頻繁に一緒に読まれるdataは埋め込み、独立してupdateされるdataは参照」という設計原則を踏まえて提案してくれ है।

Aggregation Pipeline

売ऊपर分析query

> 月別・カテゴリ別の売ऊपर集計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 } },
]);

インデックスoptimization

> इसコレクションのsearchpatternに最適なインデックスを提案して。

Claude Codeはquerypatternを分析し、複合インデックスやテキストインデックスの設計を提案してくれ है।explain() の結果を見せれば、インデックスが効いているかのconfirmも手伝ってくれ है।

validationとmiddleware

Mongooseのmiddleware(pre/post hook)やカスタムvalidationのimplementationもClaude Codeが得意とする領域 है।

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

Summary

Claude Code का उपयोग करके、MongoDBのスkeyマ設計 सेAggregation Pipelinebuild、performanceoptimization तकefficiently進められ है।SQLqueryoptimizationdatabasemigrationも合わせてreference के लिए देखें。

MongoDBके details के लिएMongoDBofficial documentationをदेखें。

#Claude Code #MongoDB #NoSQL #database #backend