MongoDB ile NoSQL Veritabanı Yönetimi
z
zafer ak
Yazar
20 November 2025
2 dakika okuma
2 görüntülenme
MongoDB temelleri, CRUD işlemleri, aggregation pipeline ve Laravel MongoDB entegrasyonu.
MongoDB Nedir?
MongoDB, document-oriented NoSQL veritabanıdır. JSON-like documents (BSON) formatında veri saklar ve schema-less yapı sunar.
SQL vs MongoDB Terminoloji
| SQL | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
| Index | Index |
| JOIN | $lookup (aggregation) |
Temel CRUD İşlemleri
// Create
db.users.insertOne({
name: "John",
email: "[email protected]",
age: 30
})
// Read
db.users.find({ age: { $gte: 18 } })
db.users.findOne({ email: "[email protected]" })
// Update
db.users.updateOne(
{ _id: ObjectId("...") },
{ $set: { age: 31 } }
)
// Delete
db.users.deleteOne({ _id: ObjectId("...") })
Query Operators
// Comparison
{ age: { $eq: 30 } } // Equal
{ age: { $gt: 25 } } // Greater than
{ age: { $in: [25, 30, 35] } }
// Logical
{ $and: [{ age: 30 }, { active: true }] }
{ $or: [{ status: "A" }, { age: { $lt: 30 } }] }
// Array
{ tags: { $all: ["php", "laravel"] } }
{ scores: { $elemMatch: { $gt: 80 } } }
Aggregation Pipeline
db.orders.aggregate([
// Stage 1: Filter
{ $match: { status: "completed" } },
// Stage 2: Group
{ $group: {
_id: "$customerId",
totalAmount: { $sum: "$amount" },
orderCount: { $sum: 1 }
}},
// Stage 3: Sort
{ $sort: { totalAmount: -1 } },
// Stage 4: Limit
{ $limit: 10 }
])
Laravel MongoDB (jenssegers/mongodb)
# Installation
composer require mongodb/laravel-mongodb
# Model
use MongoDB\Laravel\Eloquent\Model;
class Product extends Model
{
protected $connection = 'mongodb';
protected $collection = 'products';
protected $fillable = ['name', 'price', 'tags'];
}
// Query
Product::where('price', '>', 100)
->where('tags', 'electronics')
->get();
Indexing
// Single field index
db.users.createIndex({ email: 1 })
// Compound index
db.orders.createIndex({ userId: 1, createdAt: -1 })
// Text index
db.products.createIndex({ name: "text", description: "text" })
Ne Zaman MongoDB?
- Flexible, evolving schema
- Document-centric data
- Horizontal scaling gerekli
- Real-time analytics
- Content management
Sonuç
MongoDB, flexible schema ve horizontal scaling gerektiren projeler için idealdir. SQL bilgisi ile kolayca öğrenilebilir.