<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=CouchDB</id>
	<title>CouchDB - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=CouchDB"/>
	<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=CouchDB&amp;action=history"/>
	<updated>2026-04-20T11:51:34Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.joanillo.org/index.php?title=CouchDB&amp;diff=247604&amp;oldid=prev</id>
		<title>Joan a 18:37, 18 oct 2011</title>
		<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=CouchDB&amp;diff=247604&amp;oldid=prev"/>
		<updated>2011-10-18T18:37:33Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
* http://4tic.com/es/blog/40-blgo/63-couchdb-una-bd-diferente&lt;br /&gt;
* http://guide.couchdb.org/draft/tour.html&lt;br /&gt;
&lt;br /&gt;
couchdb és una base de dades diferent, orientada a url.&lt;br /&gt;
=Començant a treballar=&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install couchdb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Com a eina interactiva per treballar amb couchdb des de la consola utilitzarem ''curl''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install curl&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
       curl  is  a tool to transfer data from or to a server, using one of the&lt;br /&gt;
       supported protocols (HTTP, HTTPS, FTP, FTPS,  SCP,  SFTP,  TFTP,  DICT,&lt;br /&gt;
       TELNET,  LDAP  or  FILE).  The command is designed to work without user&lt;br /&gt;
       interaction.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Suposo que també valdria utilitzar directament ''wget''.&lt;br /&gt;
&lt;br /&gt;
Primer de tot, assegurem-nos de què couchdb està engegat:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ curl http://127.0.0.1:5984/&lt;br /&gt;
{&amp;quot;couchdb&amp;quot;:&amp;quot;Welcome&amp;quot;,&amp;quot;version&amp;quot;:&amp;quot;0.10.0&amp;quot;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
En canvi, si utilitzo wget, el que es fa és obtenir el fitxer index.html que conté aquesta informació. L'altra possibilitat, és clar, és utilitzar directament el Firefox.&lt;br /&gt;
&lt;br /&gt;
Per llistar totes les bases de dades:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ curl -X GET http://127.0.0.1:5984/_all_dbs&lt;br /&gt;
[&amp;quot;tweets-public-timeline&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
La BD tweets-public-timeline l'ha creat l'exemple amb què estava treballant (API de Twitter)&lt;br /&gt;
&lt;br /&gt;
L'opció -X representa un Request HTTP. Hi ha varies opcions: GET, POST, PUT, DELETE:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
       -X/--request &amp;lt;command&amp;gt;&lt;br /&gt;
              (HTTP) Specifies a custom request method to use when communicat‐&lt;br /&gt;
              ing  with  the  HTTP server.  The specified request will be used&lt;br /&gt;
              instead of the method otherwise used (which  defaults  to  GET).&lt;br /&gt;
              Read  the  HTTP  1.1 specification for details and explanations.&lt;br /&gt;
              Common additional HTTP requests  include  PUT  and  DELETE,  but&lt;br /&gt;
              related technologies like WebDAV offers PROPFIND, COPY, MOVE and&lt;br /&gt;
              more.&lt;br /&gt;
&lt;br /&gt;
              (FTP) Specifies a custom FTP command to use instead of LIST when&lt;br /&gt;
              doing file lists with FTP.&lt;br /&gt;
&lt;br /&gt;
              If this option is used several times, the last one will be used.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creem una base de dades:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ curl -X PUT http://127.0.0.1:5984/baseball&lt;br /&gt;
{&amp;quot;ok&amp;quot;:true}&lt;br /&gt;
&lt;br /&gt;
$ curl -X GET http://127.0.0.1:5984/_all_dbs&lt;br /&gt;
[&amp;quot;tweets-public-timeline&amp;quot;,&amp;quot;baseball&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
We should mention JavaScript Object Notation (JSON) here, the data format CouchDB speaks. JSON is a lightweight data interchange format based on JavaScript syntax. Because JSON is natively compatible with JavaScript, your web browser is an ideal client for CouchDB.&lt;br /&gt;
&lt;br /&gt;
Brackets ([]) represent ordered lists, and curly braces ({}) represent key/value dictionaries. Keys must be strings, delimited by quotes (&amp;quot;), and values can be strings, numbers, booleans, lists, or key/value dictionaries. For a more detailed description of JSON, see Appendix E, JSON Primer. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ curl -X PUT http://127.0.0.1:5984/baseball&lt;br /&gt;
{&amp;quot;error&amp;quot;:&amp;quot;file_exists&amp;quot;,&amp;quot;reason&amp;quot;:&amp;quot;The database could not be created, the file already exists.&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
$ curl -X PUT http://127.0.0.1:5984/plankton&lt;br /&gt;
{&amp;quot;ok&amp;quot;:true}&lt;br /&gt;
&lt;br /&gt;
$ curl -X DELETE http://127.0.0.1:5984/plankton&lt;br /&gt;
{&amp;quot;ok&amp;quot;:true}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=Consola d'administració. Futon=&lt;br /&gt;
Fins ara hem treballat en mode comanda amb curl. Ara  bé, ja que couchdb està orientat a url, necessitem una manera de fer totes aquestes operacions des del Firefox. La consola d'administració web es diu Futon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://127.0.0.1:5984/_utils/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
És interessant fer el ''Test Suite'' &amp;gt; ''Run All'' per comprovar que tot és correcte.&lt;br /&gt;
&lt;br /&gt;
Amb Futon és fàcil crear una base de dades (creem la bd ''hello-world''), i creem el primer document (a couchdb no hi ha taules, no és una base de dades relacional... no hi ha SQL... hi ha documents).&lt;br /&gt;
&lt;br /&gt;
Es crea el _id de forma automàtica. Quan guardo el document, també es crea el camp de la revisió (_rev).&lt;br /&gt;
&lt;br /&gt;
Per editar el Field o el Value, he de fer doble click. Per editar el Value, si és un string ho he de ficar entre doble cometes (format JSON). Amb la pestanua ''Source'' podem mirar el format JSON.&lt;br /&gt;
=Query utilitzant MapReduce=&lt;br /&gt;
La combinació de les funcions Map i Reduce es diu View/Vista en la terminologia de couchdb.&lt;br /&gt;
&lt;br /&gt;
Let’s create documents for apples, oranges, and bananas. (Allow CouchDB to generate the _id and _rev fields.) Use Futon to create documents that have a final JSON structure that looks like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;_id&amp;quot; : &amp;quot;bc2a41170621c326ec68382f846d5764&amp;quot;,&lt;br /&gt;
    &amp;quot;_rev&amp;quot; : &amp;quot;2612672603&amp;quot;,&lt;br /&gt;
    &amp;quot;item&amp;quot; : &amp;quot;apple&amp;quot;,&lt;br /&gt;
    &amp;quot;prices&amp;quot; : {&lt;br /&gt;
        &amp;quot;Fresh Mart&amp;quot; : 1.59,&lt;br /&gt;
        &amp;quot;Price Max&amp;quot; : 5.99,&lt;br /&gt;
        &amp;quot;Apples Express&amp;quot; : 0.79&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Per ficar el parell tipus de poma/preu correctament en format JSON he de ficar:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&amp;quot;fresh mart&amp;quot;: 1.59}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
i per als tres tipus de pomes:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
   &amp;quot;fresh mart&amp;quot;: 1.59,&lt;br /&gt;
   &amp;quot;Price Max&amp;quot;: 2.32,&lt;br /&gt;
   &amp;quot;Apple Express&amp;quot;: 5.4&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Anem a crear una vista, que mostri cada producte ordenat per preu.&lt;br /&gt;
&lt;br /&gt;
Creo una Temporary View on la Map Function és:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function(doc) {&lt;br /&gt;
    var shop, price, value;&lt;br /&gt;
    if (doc.item &amp;amp;&amp;amp; doc.prices) {&lt;br /&gt;
        for (shop in doc.prices) {&lt;br /&gt;
            price = doc.prices[shop];&lt;br /&gt;
            value = [doc.item, shop];&lt;br /&gt;
            emit(price, value);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
i la Reduce Function la deixo buida. Estem executant una funció Javascript sobre cadascun dels documents.&lt;br /&gt;
&lt;br /&gt;
i ara, si ho volem agrupar per productes, farem:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function(doc) {&lt;br /&gt;
    var shop, price, key;&lt;br /&gt;
    if (doc.item &amp;amp;&amp;amp; doc.prices) {&lt;br /&gt;
        for (shop in doc.prices) {&lt;br /&gt;
            price = doc.prices[shop];&lt;br /&gt;
            key = [doc.item, price];&lt;br /&gt;
            emit(key, shop);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
{{Autor}}, juliol 2011&lt;/div&gt;</summary>
		<author><name>Joan</name></author>
		
	</entry>
</feed>