Use Cases

Panduan Praktis NoSQL/MongoDB dengan Claude Code

Pelajari cara pakai NoSQL/MongoDB dengan Claude Code. Dilengkapi tips praktis dan contoh kode.

Bikin Development MongoDB Lebih Efisien dengan Claude Code

MongoDB adalah database NoSQL berbasis dokumen dengan fitur utama desain schema yang fleksibel. Pakai Claude Code, kamu bisa lebih efisien dalam merancang schema yang tepat dan membangun aggregation pipeline.

Desain Schema

Keputusan Embed vs Reference

> Desain schema untuk situs e-commerce.
> Tiga collection: user, product, order.
> Desain yang memperhatikan performa.
// Definisi schema Mongoose
import { Schema, model } from 'mongoose';

// Schema order: info produk di-embed (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: simpan nama produk saat order
    price: Number,       // Denormalized: simpan harga saat order
    quantity: { type: Number, min: 1 },
  }],
  totalAmount: { type: Number, required: true },
  status: {
    type: String,
    enum: ['pending', 'confirmed', 'shipped', 'delivered', 'cancelled'],
    default: 'pending',
  },
  shippingAddress: {
    postalCode: String,
    province: String,
    city: String,
    line1: String,
    line2: String,
  },
}, { timestamps: true });

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

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

Claude Code bakal ngasih saran berdasarkan prinsip desain: “data yang sering dibaca bareng di-embed, data yang di-update independen pakai reference”.

Aggregation Pipeline

Query Analisis Penjualan

> Buat aggregation pipeline untuk agregasi penjualan per bulan dan per kategori.
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 } },
]);

Optimasi Index

> Sarankan index yang optimal untuk pola pencarian di collection ini.

Claude Code menganalisis pola query dan menyarankan desain compound index atau text index. Kalau kamu tunjukin hasil explain(), dia juga bakal bantu konfirmasi apakah index-nya memang efektif.

Validasi dan Middleware

Implementasi middleware Mongoose (pre/post hook) dan validasi custom juga bidang yang jadi kekuatan Claude Code.

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

Summary

Dengan Claude Code, mulai dari desain schema MongoDB, pembangunan aggregation pipeline, sampai optimasi performa, semuanya bisa kamu kerjain dengan efisien. Baca juga optimasi SQL query dan migrasi database untuk referensi.

Untuk detail MongoDB, lihat dokumentasi resmi MongoDB.

#Claude Code #MongoDB #NoSQL #database #backend

Tingkatkan alur kerja Claude Code kamu

50 template prompt yang sudah teruji, siap copy-paste ke Claude Code sekarang juga.

Gratis

PDF Gratis: Cheatsheet Claude Code dalam 5 Menit

Perintah penting, pintasan, dan contoh prompt dalam satu halaman siap cetak.

Unduh PDF
M

Tentang Penulis

Masa

Engineer yang aktif menggunakan Claude Code. Mengelola claudecode-lab.com, media teknologi 10 bahasa dengan lebih dari 2.000 halaman.