Apache CouchDB
NoSQLEn document-oriented NoSQL database der bruger JSON for data, JavaScript for queries, og HTTP for API.
Beskrivelse
CouchDB er en document-oriented database designet fra grunden til web. Den gemmer data som JSON documents, bruger MapReduce written i JavaScript til queries og views, og eksponerer alt via en RESTful HTTP API - ingen special drivers nødvendige. CouchDB's motto er 'relax' fordi den er designet til at være nem at bruge og distribuere. En af dens stærkeste features er multi-master replication med automatic conflict detection og resolution - perfekt til offline-first mobile apps. CouchDB bruger MVCC (Multi-Version Concurrency Control) som betyder at readers aldrig blokerer writers, og vice versa. Den har ingen query language som SQL - i stedet definerer du views ved hjælp af MapReduce functions, som pre-kompileres til incrementally updated indexes. CouchDB's architecture gør den excellent til scenarios hvor data skal synkroniseres mellem multiple devices (mobile apps) eller datacenters. PouchDB er en JavaScript implementation der kan køre i browseren og sync med CouchDB.
Features
- •JSON document storage
- •RESTful HTTP API
- •Multi-master replication
- •Automatic conflict detection
- •MVCC (no locks)
- •MapReduce views
- •Change notifications feed
- •Attachments support (binary files)
Query Eksempel
// CouchDB bruger HTTP API og MapReduce views
// 1. Opret database (HTTP PUT)
PUT http://localhost:5984/products
// 2. Indsæt document (HTTP POST)
POST http://localhost:5984/products
Content-Type: application/json
{
"_id": "laptop-001",
"type": "product",
"name": "Lenovo ThinkPad",
"price": 8999,
"category": "laptops",
"stock": 25,
"tags": ["electronics", "computers"]
}
// 3. Hent document (HTTP GET)
GET http://localhost:5984/products/laptop-001
// 4. Definer view med MapReduce (design document)
PUT http://localhost:5984/products/_design/products
{
"_id": "_design/products",
"views": {
"by_category": {
"map": "function(doc) { if (doc.category) { emit(doc.category, doc.price); } }",
"reduce": "_sum"
},
"by_price": {
"map": "function(doc) { if (doc.price) { emit(doc.price, doc); } }"
},
"expensive_products": {
"map": "function(doc) { if (doc.price > 5000) { emit(doc.name, doc.price); } }"
}
}
}
// 5. Query view
GET http://localhost:5984/products/_design/products/_view/by_category
// 6. Query med parameters
GET http://localhost:5984/products/_design/products/_view/by_category?key="laptops"
// 7. Range query
GET http://localhost:5984/products/_design/products/_view/by_price?startkey=5000&endkey=10000
// 8. Update document
PUT http://localhost:5984/products/laptop-001
{
"_id": "laptop-001",
"_rev": "1-967a00dff5e02add41819138abb3284d",
"type": "product",
"name": "Lenovo ThinkPad",
"price": 7999,
"category": "laptops",
"stock": 20
}
// 9. Delete document
DELETE http://localhost:5984/products/laptop-001?rev=2-abc123...
// 10. Continuous changes feed
GET http://localhost:5984/products/_changes?feed=continuous
// JavaScript (Node.js) example med nano library
const nano = require('nano')('http://localhost:5984');
const db = nano.db.use('products');
// Insert
await db.insert({
name: 'MacBook Pro',
price: 15999,
category: 'laptops'
});
// Query view
const result = await db.view('products', 'by_category', {
key: 'laptops'
});
console.log(result.rows);Anvendelsesområder
- •Offline-first mobile applications
- •Multi-device data synchronization
- •Content management systems
- •User profile og settings storage
- •Distributed systems med eventual consistency
Fordele
- ✓Ingen special drivers - bare HTTP
- ✓Excellent offline/sync capabilities
- ✓Schema-less flexibility
- ✓Built-in replication og clustering
- ✓Web-friendly (JavaScript, JSON, HTTP)
Ulemper
- ✗MapReduce kan være komplekst
- ✗Ikke optimal til complex queries
- ✗Views skal pre-defineres
- ✗Langsommere end nogle andre NoSQL databases
- ✗Begrænset ad-hoc query support
Bedst til
- →Offline-first mobile apps med sync
- →Multi-device applications
- →Content management med versioning
- →Distributed systems med eventual consistency
- →Web applications (JavaScript stack)
Ikke anbefalet til
- ⚠Complex relational queries
- ⚠ACID transactions across documents
- ⚠Real-time analytics
- ⚠High-frequency trading systems
- ⚠Applications requiring strong consistency