Couchdb Tutorial PDF
Couchdb Tutorial PDF
This tutorial provides a brief knowledge about CouchDB, the procedures to set it
up, and the ways to interact with CouchDB server using cURL and Futon. It also
tells how to create, update and delete databases and documents.
Audience
This tutorial helps the professionals aspiring to make a career in Big Data and
NoSQL databases, especially the documents store.
Prerequisites
Before you start proceeding with this tutorial, we are assuming that you have a
brief knowledge on Big Data, Hadoop, and also have the basic understanding of
databases.
Table of Contents
About the Tutorial ..................................................................................................................................... i
Audience .................................................................................................................................................... i
Prerequisites .............................................................................................................................................. i
Copyright & Disclaimer .............................................................................................................................. i
Table of Contents ...................................................................................................................................... ii
1. INTRODUCTION..................................................................................................................... 1
RDBMS ...................................................................................................................................................... 1
OLAP ......................................................................................................................................................... 1
NoSQL Databases ...................................................................................................................................... 1
What is CouchDB? ..................................................................................................................................... 2
Why CouchDB? ......................................................................................................................................... 2
Data Model ............................................................................................................................................... 2
Features of CouchDB: Reduce the Content................................................................................................ 3
History ...................................................................................................................................................... 4
2. INSTALLATION ....................................................................................................................... 5
Installing CouchDB in Windows................................................................................................................. 5
Installing CouchDB in Linux Systems ......................................................................................................... 7
ii
5. CREATING A DATABASE....................................................................................................... 25
Creating a Database using cURL Utility.................................................................................................... 25
Creating a Database using Futon ............................................................................................................. 26
9. DELETING A DOCUMENT..................................................................................................... 40
Deleting a Document using cURL Utility .................................................................................................. 40
Deleting a Document using Futon ........................................................................................................... 41
iii
iv
1. INTRODUCTION
CouchDB
RDBMS
RDBMS stands for Relational Database Management System. RDBMS is the basis
for SQL, and for all modern database systems like MS SQL Server, IBM DB2,
Oracle, MySQL, and Microsoft Access.
A Relational database management system (RDBMS) is a database management
system (DBMS) that is based on the relational model as introduced by E. F.
Codd.
The data in RDBMS is stored in database objects called tables. The table is a
collection of related data entries and it consists of columns and rows. It stores
only structured data.
OLAP
Online Analytical Processing Server (OLAP) is based on the multidimensional
data model. It allows managers and analysts to get an insight of the information
through fast, consistent, and interactive access to information.
NoSQL Databases
A NoSQL database (sometimes called as Not Only SQL) is a database that
provides a mechanism to store and retrieve data other than the tabular relations
used in relational databases. These databases are schema-free, support easy
replication, have simple API, eventually consistent, and can handle huge
amounts of data (big data).
The primary objective of a NoSQL database is to have the following:
Simplicity of design,
CouchDB
etc. These NoSQL databases are classified into three types and they are
explained below.
Key-value Store: These databases are designed for storing data in key-value
pairs and these databases will not have any schema. In these databases, each
data value consists of an indexed key and a value for that key.
Examples: BerkeleyDB, Cassandra, DynamoDB, Riak.
Document Store: These are the databases developed on the basic idea of keyvalue stores where documents contain more complex data. Here, each
document is assigned a unique key, which is used to retrieve the document.
These are designed for storing, retrieving, and managing document-oriented
information, also known as semi-structured data.
Examples: CouchDB and MongoDB.
What is CouchDB?
CouchDB is an open source database developed by Apache software foundation.
The focus is on the ease of use, embracing the web. It is a NoSQL document
store database.
It uses Json, to store data (documents), java script as its query language to
transform the documents, http protocol for api to access the documents, query
the indices with the web browser. It is a multi master application released in
2005 and it became an apache project in 2008.
Why CouchDB?
Data Model
CouchDB
ACID Properties
CouchDB contains ACID properties as one of its features.
Consistency: When the data in CouchDB was once committed, then this data will
not be modified or overwritten. Thus, CouchDB ensures that the database file
will always be in a consistent state.
A multi-Version Concurrency Control (MVCC) model is used by CouchDB reads,
because of which the client will see a consistent snapshot of the database from
the beginning to the end of the read operation.
CouchDB
Whenever a documents is updated, CouchDB flushes the data into the disk, and
the updated database header is written in two consecutive and identical chunks
to make up the first 4k of the file, and then synchronously flushed to disk.
Partial updates during the flush will be discarded.
If the failure occurred while committing the header, a surviving copy of the
previous identical headers will remain, ensuring coherency of all previously
committed data. Except the header area, consistency checks or fix-ups after a
crash or a power failure are never necessary.
Compaction
Whenever the space in the database file got wasted above certain extent, all the
active data will be copied (cloned) to a new file. When the copying process is
entirely done, then the old file will be discarded. All this is done by compaction
process. The database remains online during the compaction and all updates and
reads are allowed to complete successfully.
Views
Data in CouchDB is stored in semi-structured documents that are flexible with
individual implicit structures, but it is a simple document model for data storage
and sharing. If we want see our data in many different ways, we need a way to
filter, organize and report on data that hasnt been decomposed into tables.
To solve this problem, CouchDB provides a view model. Views are the method of
aggregating and reporting on the documents in a database, and are built on
demand to aggregate, join and report on database documents. Because views
are built dynamically and dont affect the underlying document, you can have as
many different view representations of the same data as you like.
History
2. INSTALLATION
CouchDB
This chapter teaches you how to install CouchDB in windows as well as Linux
systems.
If you click on the download button that will lead to a page where download links
of CouchDB in various formats are provided. The following snapshot illustrates
the same.
CouchDB
Choose the download link for windows systems and select one of the provided
mirrors to start your download.
Installing CouchDB
CouchDB will be downloaded to your system in the form of setup file named
setup-couchdb-1.6.1_R16B02.exe. Run the setup file and proceed with the
installation.
After installation, open built in web interface of CouchDB by visiting the following
link: http://127.0.0.1:5984/. If everything goes fine, this will give you a web
page, which have the following output.
{
"couchdb":"Welcome","uuid":"c8d48ac61bb497f4692b346e0f400d60",
"version":"1.6.1",
"vendor":{
"version":"1.6.1","name":"The Apache Software Foundation"
CouchDB
You can interact with the CouchDB web interface by using the following url:
http://127.0.0.1:5984/_utils/
This shows you the index page of Futon, which is the web interface of CouchDB.
Erlang OTP
ICU
OpenSSL
7
CouchDB
Mozilla SpiderMonkey
GNU Make
GNU Compiler Collection
libcurl
help2man
Python for docs
Python Sphinx
yum
yum
yum
yum
yum
yum
yum
yum
yum
yum
yum
yum
yum
yum
install
install
install
install
install
install
install
install
install
install
install
install
install
install
autoconf
autoconf-archive
automake
curl-devel
erlang-asn1
erlang-erts
erlang-eunit
erlang-os_mon
erlang-xmerl
help2man
js-devel
libicu-devel
libtool
perl-Test-Harness
Note: For all these commands you need to use sudo. The following procedure
converts a normal user to a sudoer.
Then edit as shown below to give your existing user the sudoer privileges:
Hadoop All=(All) All , and press esc : x to write the changes to
the file.
Downloading CouchDB
Apache software foundation will not provide the complete .tar file for CouchDB,
so you have to install it from the source. Download the source file of CouchDB
from the following link:
8
CouchDB
www.apache.org/dist/couchdb/source/1.6.1/apache-couchdb-1.6.1.tar.gz.
Create a new directory to install CouchDB, browse to such created directory and
download CouchDB source by executing the following commands:
$ cd
$ mkdir CouchDB
$ cd CouchDB/
$ wget
http://www.google.com/url?q=https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.apache.org%2Fdist%2Fcouchdb
%2Fsource%2F1.6.1%2Fapache-couchdb-1.6.1.tar.gz
This will download CouchDB source file into your system. Now unzip the
apache-couchdb-1.6.1.tar.gz as shown below.
$ tar zxvf apache-couchdb-1.6.1.tar.gz
Configuring CouchDB
To configure CouchDB, do the following:
--with-erlang=/usr/lib64/erlang/usr/include/
It gives you the following output similar to that shown below with a concluding
line saying - You have configured Apache CouchDB, time to relax.
# ./configure
--with-erlang=/usr/lib64/erlang/usr/include/
CouchDB
Installing CouchDB
Now type the following command to install CouchDB in your system.
# make && sudo make install
It installs CouchDB in your system with a concluding line saying - You have
installed Apache CouchDB, time to relax.
Starting CouchDB
To start CouchDB, browse the directory in CouchDB home folder of CouchDB and
use the following command:
$ cd apache-couchdb-1.6.1
$ cd etc
$ couchdb start
It starts CouchDB giving the following output:
Apache CouchDB 1.6.1 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [<0.31.0>] Apache CouchDB has started on http://127.0.0.1:5984/
[info] [<0.112.0>] 127.0.0.1 - - GET / 200
[info] [<0.112.0>] 127.0.0.1 - - GET /favicon.ico 200
10
CouchDB
Verification
Since CouchDB is a web interface, try to type the following homepage url in the
browser.
http://127.0.0.1:5984/
It produces the following output:
{
"couchdb":"Welcome",
"uuid":"8f0d59acd0e179f5e9f0075fa1f5e804",
"version":"1.6.1",
"vendor":{
"name":"The Apache Software Foundation",
"version":"1.6.1"
11
CouchDB
cURL Utility
cURL utility is a way to communicate with CouchDB.
It is a tool to transfer data from or to a server, using one of the supported
protocols (HTTP, HTTPS, FTP, FTPS, TFTP, DICT, TELNET, LDAP or FILE). The
command is designed to work without user interaction. cURL offers a busload of
useful tricks like proxy support, user authentication, ftp upload, HTTP post, SSL
(https:) connections, cookies, file transfer resume and more.
The cURL utility is available in operating systems such as UNIX, Linux, Mac OS X
and Windows. It is a command line utility using which user can access HTTP
protocol straight away from the command line. This chapter teaches you how to
use cURL utility.
12
CouchDB
-d/--data <data>
--data-ascii <data>
--disable-eprt
--disable-epsv
-G/--get
-h/--help
--hostpubmd5 <md5> Hex encoded MD5 string of the host public key.
(SSH)
-0/--http1.0
--ignore-content-length
-i/--include
-M/--manual
<pass>
CouchDB
--post301
redirect (H)
--post302
redirect (H)
-O/--remote-name
-X flag
(HTTP) Specifies a custom request method used when communicating with the
HTTP server. The specified request is used instead of the method otherwise used
(which defaults to GET). Read the HTTP 1.1 specification for details and
explanations.
(FTP) Specifies a custom FTP command to use instead of LIST when doing file
lists with ftp.
-H
(HTTP) Extra header is used when getting a web page. Note that if you add a
custom header that has the same name as one of the internal ones cURL would
use, your externally set header will be used instead of the internal one. This
allows you to make even trickier work than cURL would normally do. You should
not replace internally set headers without perfectly knowing what youre doing.
Replacing an internal header with the one without content on the right side of
the colon, will prevent that header from appearing.
14
CouchDB
cURL assures that each header you add/replace get sent with the proper end of
line marker. Neither you should add that as a part of the header content nor add
newlines or carriage returns to disorder things.
See also the -A/--user-agent and -e/--referer options.
This option can be used multiple times to add/replace/remove multiple headers.
-d flag
Using this tag of cURL, you can send data along with the HTTP POST request to
the server, as if it was filled by the user in the form and submitted.
Example
Suppose there is a website and you want to login into it or send some data to
the website using d flag of cURL utility as shown below.
curl -X PUT http://mywebsite.com/login.html -d userid=001 -d
password=tutorialspoint
It
sends
a
post
chunk
that
userid=001&password=tutorialspoint. Likewise you
documents (JSON ) using -d flag.
looks
can also
like
send
-o flag
Using this flag, cURL writes the output of the request to a file.
Example
The following example shows the use of -o flag of cURL utility.
$ curl -o example.html www.tutorialspoint.com/index.htm % Total %
Received % Xferd Average Speed Time
Time Time Current
Dload Upload
100 81193 0 81193
58077
Total
0 48168
Spent
Left Speed
This gets the source code of the homepage of tutorialspoint.com, creates a file
named example.com and saves the output in the file named example.html.
Following is the snapshot of the example.html.
15
CouchDB
-O
This flag is similar to o. the only difference is with this flag, a new file with the
same name as the requested url was created, and the source code of the
requested url will be copied to it.
Example
The following example shows the use of -O flag of cURL utility.
$ curl -O www.tutorialspoint.com/index.htm
% Total % Received % Xferd
Average Speed
Dload
Time Time
Upload
Total
Time
Current
Spent Left
Speed
100 81285 0 81285
60077
49794
0 --:--:--
0:00:01 --:--:--
It creates a new file with the name index.htm and saves the source code of the
index page of tutorialspoint.com in it.
Hello CouchDB
You can access the homepage of the CouchDB by sending a GET request to the
CouchDB instance installed. First of all make sure you have installed CouchDB in
your Linux environment and it is running successfully, and then use the following
syntax to send a get request to the CouchDB instance.
16
CouchDB
curl http://127.0.0.1:5984/
This gives you a JSON document as shown below where CouchDB specifies the
details such as version number, name of the vendor, and version of the
software.
$ curl http://127.0.0.1:5984/
{
"couchdb" :
"Welcome",
"uuid"
"version"
"vendor"
"8f0d59acd0e179f5e9f0075fa1f5e804",
"1.6.1",
{
Creating a Database
You can create a database in CouchDB using cURL with PUT header using the
following syntax:
$ curl -X PUT http://127.0.0.1:5984/ database name
Example
As an example, using the above given syntax create a database with name
my_database as shown below.
$ curl -X PUT http://127.0.0.1:5984/my_database
17
CouchDB
{"ok":true}
Verification
Verify whether the database is created, by listing out all the databases as shown
below. Here you can observe the name of newly created database,
"my_database" in the list.
$ curl -X GET http://127.0.0.1:5984/_all_dbs
Example
As an example let us get the information of the database named my_database
as shown below. Here you can get the information about your database as a
response.
$ curl -X GET http://127.0.0.1:5984/my_database
{
" db_name " : " my_database ",
" doc_count " : 0,
" doc_del_count " : 0,
" update_seq " : 0,
" purge_seq " : 0,
" compact_running " :f alse,
" disk_size " : 79,
" data_size " : 0,
" instance_start_time " : "1423628520835029",
" disk_format_version " : 6,
" committed_update_seq " : 0
18
CouchDB
Futon
Futon is the built-in, web based, administration interface of CouchDB. It provides
a simple graphical interface using which you can interact with CouchDB. It is a
naive interface and it provides full access to all CouchDB features. Following is
the list of those features.
Databases
Creates databases.
Destroys databases.
Documents
Creates documents.
Updates documents.
Edits documents.
Deletes documents.
Starting Futon
To start Futon, install CouchDB in your system, make sure it is running
successfully, and in any of the browsers open the following url.
http://127.0.0.1:5984/_utils/
If you open this url, it displays the Futon home page as shown below.
19
CouchDB
On the left hand side of this page you can observe the list of all the
current databases of CouchDB. In this illustration, we have a database
named my_database, along with system defined databases _replicator
and _user.
the
complete
Documentation:
Recent Databases: Under this you can find the names of recently
added databases.
20
CouchDB
4. HTTP API
Using HTTP request headers, you can communicate with CouchDB. Through
these requests we can retrieve data from the database, store data in to the
database in the form of documents, and we can view as well as format the
documents stored in a database.
request
formats
of
HTTP
Protocol used
to
GET: This format is used to get a specific item. To get different items, you
have to send specific url patterns. In CouchDB using this GET request, we
can get static items, database documents and configuration, and
statistical information in the form of JSON documents (in most cases).
HEAD: The HEAD method is used to get the HTTP header of a GET request
without the body of the response.
PUT: Using PUT request, you can create new objects, databases,
documents, views and design documents.
DELETE: Using DELETE request, you can delete documents, views, and
design documents.
COPY: Using COPY method, you can copy documents and objects.
Content-type: This Header is used to specify the content type of the data
that we supply to the server along with the request. Mostly the type of the
content we send along with the request will be MIME type or JSON
21
CouchDB
Response Headers
These are the headers of the response sent by the server. These headers give
information about the content send by the server as response.
Content-type: This header specifies the MIME type of the data returned by
the server. For most request, the returned MIME type is text/plain.
Content-length: This header returns the length of the content sent by the
server, in bytes.
Etag: This header is used to show the revision for a document, or a view.
Status Codes
Following is the tabular form of the status code sent by the http header and the
description of it.
200 - OK
201 - Created
202 - Accepted
22
CouchDB
405
Allowed
Resource
409 - Conflict
500 Error
Internal
Operation
PUT /db
GET /db
PUT /db/document
GET /db/document
This url is
document.
DELETE /db/document
GET /db/_design/design-doc
used
to get the
the existing
to
create a
existing
get
the
delete the
from
the
23
CouchDB
GET
/db/_design/design- This url is used to access the view,
doc/_view/view-name
view-name
from
the
design
document
from
the
specified
database.
24
CouchDB
5. CREATING A DATABASE
Database is the outermost data structure in CouchDB where your documents are
stored. You can create these databases using cURL utility provided by CouchDB,
as well as Futon the web interface of CouchDB. This chapter teaches you the
ways to create a database.
Example
Using the above given syntax if you want to create a database with name
my_database, you can create it as follows:
curl -X PUT http://127.0.0.1:5984/my_database
"ok":true
}
As a response the server will return you a JSON document with content ok :
true indicating the operation was successful.
Verification
Verify whether the database is created, by listing out all the databases as shown
below. Here you can observe the name of a newly created database, "
my_database " in the list.
25
CouchDB
In this page, you can see the list of databases in CouchDB, an option button
Create Database on the left hand side.
Now click on the create database link. You can see a popup window Create New
Databases asking for the database name for the new database. Choose any
name following the mentioned criteria. Here we are creating another database
with name tutorials_point. Click on the create button as shown in the following
screenshot.
26
CouchDB
27
CouchDB
6. DELETING A DATABASE
Deleting a Database using cURL Utility
You can delete a database in CouchDB by sending a request to the server using
DELETE method through cURL utility. Following is the syntax to create a
database.
$ curl -X DELETE http://127.0.0.1:5984/ database name
Using -x we can specify a custom request method of HTTP we are using, while
communicating with the HTTP server. In this case, we are using the DELETE
method. Send the url to the server by specifying the database to be deleted in
it.
Example
Assume there is a database named my_database2 in CouchDB. Using the above
given syntax if you want to delete it, you can do it as follows:
$ curl -X DELETE http://127.0.0.1:5984/my_database2
{
"ok" : true
}
As a response, the server will return you a JSON document with content ok :
true indicating the operation was successful.
Verification
Verify whether the database is deleted by listing out all the databases as shown
below. Here you can observe the name of the deleted database,
"my_database" is not there in the list.
$ curl -X GET http://127.0.0.1:5984/_all_dbs
28
CouchDB
Here you can see three user created databases. Let us delete the database
named tutorials_point2. To delete a database, select one from the list of
databases, and click on it, which will lead to the overview page of the selected
database where you can see the various operations on databases. The following
screenshot shows the same:
29
CouchDB
Among them you can find Delete Database option. By clicking on it you will get
a popup window, asking whether you are sure! Click on delete, to delete the
selected database.
30
CouchDB
7. CREATING A DOCUMENT
Documents are CouchDBs central data structure. Contents of the database will
be stored in the form of Documents instead of tables. You can create these
documents using cURL utility provided by CouchDB, as well as Futon. This
chapter teaches you the ways to create a document in a database.
Each document in CouchDB has a unique ID. You can choose your own ID that
should be in the form of a string. Generally, UUID (Universally Unique IDentifier)
is used, which are random numbers that have least chance of creating a
duplicate. These are preferred to avoid collision.
{
Name : Raju
age : 23
Designation : Designer
}
Example
31
CouchDB
Using the above given syntax if you want to create a document with id 001 in a
database with name my_database, you can create it as shown below.
$ curl -X PUT http://127.0.0.1:5984/my_database/ "001" -d
'{ " Name " : " Raju " , " age :" 23 " , Designation : Designer
}'
{"ok":true,"id":"001","rev":"1-1c2fae390fa5475d9b809301bbf3f25e"}
The response of CouchDB to this request contains three fields:
rev, This indicates the revision id. Every time you revise (update or
modify) a document a _rev value will be generated by CouchDB. If you
want to update or delete a document, CouchDB expects you to include the
_rev field of the revision you wish to change. When CouchDB accepts the
change, it will generate a new revision number. This mechanism ensures
concurrency control.
Verification
If you want to view the created document you can get it using the document as
shown below.
$ curl -X GET http://127.0.0.1:5984/my_database/ 001
{
"_id": "001",
"_rev": "1-3fcc78daac7a90803f0a5e383f4f1e1e",
"Name": "Raju",
"age": 23,
"Designation": "Designer"
}
32
CouchDB
Select the database in which you want to create the document. Open the
Overview page of the database and select New Document option as shown
below.
When you select the New Document option, CouchDB creates a new database
document, assigning it a new id. You can edit the value of the id and can assign
your own value in the form of a string. In the following illustration, we have
created a new document with an id 001.
33
CouchDB
In this page, you can observe three options - save Document, Add Field and
Upload Attachment.
34
CouchDB
Save Document
You can save the changes made to the document by clicking on this option. After
saving, a new id _rev will be generated as shown below.
35
CouchDB
8. UPDATING A DOCUMENT
Updating Documents using cURL
Example
Suppose there is a document with id 001 in the database named my_database.
You can delete this as shown below.
First of all, get the rev id of the document that is to be updated. You can find the
_rev of the document in the document itself, therefore get the document as
shown below.
$ curl -X GET http://127.0.0.1:5984/my_database/001
{
" _id " : " 001 ",
" _rev
{ " ok " : true , " id " : " 001 " , " rev " : " 204d8eac1680d237ca25b68b36b8899d3 " }
Verification
To verify the document, get the document again using GET request as shown
below.
36
CouchDB
Note
Following are some important points to be noted while updating a document.
The URL we send in the request containing the database name and the
document id.
The returned JSON contains the success message, the ID of the document
being updated, and the new revision information. If you want to update
the new version of the document, you have to quote this new revision
number.
37
CouchDB
Select the database in which the document to be updated exists and click it.
Here we are updating a document in the database named tutorials_point. You
will get the list of documents in the database as shown below.
Select a document that you want to update and click on it. You will get the
contents of the documents as shown below.
38
CouchDB
Here, to update the location from Delhi to Hyderabad, click on the text box, edit
the field, and click the green button to save the changes as shown below.
39
CouchDB
9. DELETING A DOCUMENT
Deleting a Document using cURL Utility
Example
Suppose there is a document in database named my_database with document
id 001. To delete this document, you have to get the rev id of the document. Get
the document data as shown below.
$ curl -X GET http://127.0.0.1:5984/my_database/001
{
" _id " : " 001 ",
" _rev
{"ok":true,"id":"001","rev":"2-3a561d56de1ce3305d693bd15630bf96"}
40
CouchDB
Verification
To verify whether the document is deleted, try to fetch the document by using
the GET method. Since you are fetching a deleted document, this will give you
an error message as shown below.
$ curl -X GET http://127.0.0.1:5984/my_database/001
{"error":"not_found","reason":"deleted"}
Here you can observe, the database consists of three documents. To delete any
of the documents say 003, do the following:
Click on the document, you will get a page showing the contents of
selected document in the form of field-value pairs.
This page also contains four options namely Save Document , Add
Field, Upload Attachment, Delete Document.
CouchDB
You will get a dialog box saying Are you sure you want to delete this
document? Click on delete, to delete the document.
42
CouchDB
--data-binary@: This option tells cURL to read a files contents into the
HTTP request body.
-H: This option is used to mention the content type of the file we are going
to upload.
Example
Let us attach a file named boy.jpg, to the document with id 001, in the
database named my_database by sending PUT request to CouchDB. Before
that, you have to fetch the data of the document with id 001 to get its current
rev id as shown below.
$ curl -X GET http://127.0.0.1:5984/my_database/001
{
"_id": "001",
"_rev": "1-967a00dff5e02add41819138abb3284d"
}
Now using the _rev value, send the PUT request to the CouchDB server as
shown below.
$ curl -vX PUT http://127.0.0.1:5984/my_database/001/boy.jpg?rev=1967a00dff5e02add41819138abb3284d --data-binary @boy.jpg -H "ContentType: image/jpg"
43
CouchDB
Verification
To verify whether the attachment is uploaded, fetch the document content as
shown below.
$ curl -X GET http://127.0.0.1:5984/my_database/001
{
"_id": "001",
"_rev": "2-4705a219cdcca7c72aac4f623f5c46a8",
"_attachments": {
"boy.jpg": {
"content_type": "image/jpg",
"revpos": 2,
"digest": "md5-9Swz8jvmga5mfBIsmCxCtQ==",
"length": 91408,
"stub": true
}
}
}
44
CouchDB
The file uploaded will be displayed under _attachments field. Later you can see
the file by clicking on it.
45