Use Cases

NoSQL/MongoDB Practical Guide dengan Claude Code

Pelajari tentang nosql/mongodb practical guide menggunakan Claude Code. Dilengkapi tips praktis dan contoh kode.

MongoDBpengembangan dengan Claude Code: efisiensi

MongoDB dokumen指向 NoSQLdatabase 、fleksibelなスキーマ設計 fitur.Claude Code 使えば、tepatなスキーマ設計やAggregation Pipeline pembangunan 効率よく進められ.

Desain Skema

埋め込み vs 参照の判断

> ECサイト スキーマ 設計して。
> pengguna、商品、注文 3コレクション。
> performa 考慮した設計 dengan 。
// Mongooseスキーマdefinisi
import { Schema, model } from 'mongoose';

// 注文スキーマ:商品情報 埋め込み(非正規化)
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 });

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

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

Claude Code 「頻繁 一緒 読まデータ 埋め込み、独立 pembaruanさデータ 参照」 dan いう設計原則 踏まえて提案 くれ.

Aggregation Pipeline

売上分析query

> 月別・kategori別 売上集計Aggregation Pipeline buatkan.
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 } },
]);

indexoptimasi

> こ コレクション pencarianpola 最適なindex 提案して。

Claude Code querypola 分析し、複合indexやテキストindex 設計 提案 くれ.explain() 結果 見せれば、index 効いてか konfirmasi juga 手伝ってくれ.

validasiとmiddleware

Mongoose middleware(pre/post フック)やカスタムvalidasi implementasi juga Claude Code 得意 dan 領域.

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

Summary

Dengan Claude Code, MongoDB スキーマ設計 dari Aggregation Pipelinepembangunan、performaoptimasiま efisien 進められ.SQLqueryoptimasidatabaseマイグレーション juga 合わせて参考 .

Untuk MongoDBの詳細, lihat MongoDB公式ドキュメント.

#Claude Code #MongoDB #NoSQL #database #backend