Partie Théorique
À la fin du processus, le nœud d'origine peut recomposer une réponse au problème qui lui avait été soumis :
reduce(cle2, list(valeur2))→ list(valeur2)
Subdivision <-> Agrégation
Partie Pratique
$ apt-get install couchdb
$ apt-get install curl
$ curl -X GET http://localhost:5984
{
"couchdb":"Welcome",
"uuid":"08a4ff9b3d2da0142aad1f625a1faf6c",
"version":"1.5.0",
"vendor":
{
"version":"14.04",
"name":"Ubuntu"
}
}
$ curl -X PUT http://localhost:5984/contract (x2)
...
$ curl -X GET http://localhost:5984/_all_dbs
...
$ curl -X DELETE http://127.0.0.1:5984/contract
...
$ curl -X PUT http://localhost:5984/contract
...$ curl -X PUT http://localhost:5984/contract/contract-1 -d '{
"id": 56405,
"contract_name": "Formule - 1/1",
"gamme": "Santé",
"formule_name": "Formule ANI",
"distributors": ["distrib-a", "distrib-b", "distrib-c"],
"created_by": "Alan Brax",
"created_at": "10/03/2015 à 15:42",
"updated_by": "Bobby Wallace",
"is_active": true
}'
# status : ok
# _id : l'identifiant
# _rev : numéro de révision
# deux fois
$ curl -X GET http://localhost:5984/contract/contract-1
# MODIFICATION SIMPLE
$ curl -X PUT http://localhost:5984/contract/contract-1 -d '{
"_id": "contract-1",
"_rev": "2-18ba7f42526a2c1d20485e629bc67dbf",
"id": 56405,
"contract_name": "Formule - 1/1",
"gamme": "Santé",
"formule_name": "Formule ANI",
"distributors": ["distrib-a", "distrib-b", "distrib-c"],
"created_by": "Alan Brax",
"created_at": "15/05/2015 à 15:42",
"updated_by": "Bobby Wallace",
"is_active": true
}'
#ESSAYONS AVEC LE MEME _REV ?
#ESSAYONS AVEC LE MEME _ID SANS _REV ?
#ESSAYONS EN SUPPRIMANT DES LIGNES ?
$ curl -X GET http://localhost:5984/contract/contract-1
# MODIFICATION SIMPLE
$ curl -X DELETE http://localhost:5984/contract/contract-1 (2 fois)GO : http://localhost:5984/_utils/
$ curl -X PUT http://localhost:5984/test-magasin/pomme -d '{
"item": "pomme",
"prices": {
"Carrefour": 1.59,
"Auchan": 5.99,
"Aldi": 0.79
}
}'
$ curl -X PUT http://localhost:5984/test-magasin/orange -d '{
"item": "orange",
"prices": {
"Carrefour": 1.99,
"Auchan": 3.19,
"Aldi": 1.09
}
}'
$ curl -X PUT http://localhost:5984/test-magasin/banane -d '{
"item": "banane",
"prices": {
"Carrefour": 1.99,
"Price Max": 0.79,
"Aldi": 4.22
}
}'
function(doc) {
var shop, price, value;
if (doc.item && doc.prices) {
for (shop in doc.prices) {
price = doc.prices[shop];
value = [doc.item, shop];
emit(price, value);
}
}
}function(doc) {
var shop, price, key;
if (doc.item && doc.prices) {
for (shop in doc.prices) {
price = doc.prices[shop];
key = [doc.item, price];
emit(key, shop);
}
}
}function(doc) {
var banane, pomme, orage;
if (doc.item && doc.prices) {
for (shop in doc.prices) {
price = doc.prices[shop];
value = [doc.item, shop];
emit(doc.item, price);
}
}
}function (keys, prices) {
return sum(prices);
}
function(keys, values, rereduce) {
if (rereduce) {
return {
'sum': values.reduce(function(a, b) { return a + b.sum }, 0),
'min': values.reduce(function(a, b) { return Math.min(a, b.min) }, Infinity),
'max': values.reduce(function(a, b) { return Math.max(a, b.max) }, -Infinity),
'count': values.reduce(function(a, b) { return a + b.count }, 0),
'sumsqr': values.reduce(function(a, b) { return a + b.sumsqr }, 0)
}
} else {
return {
'sum': sum(values),
'min': Math.min.apply(null, values),
'max': Math.max.apply(null, values),
'count': values.length,
'sumsqr': (function() {
var sumsqr = 0;
values.forEach(function (value) {
sumsqr += value * value;
});
return sumsqr;
})(),
}
}
}
$ curl -X GET http://localhost:5984/test-magasin/_design/bobby/_view/bobby\?group\=true