Your cart is currently empty!
Category: couchdb
-
Khóa học miễn phí CouchDB – Creating a Document nhận dự án làm có lương
CouchDB – Creating a Document
Documents are CouchDB’s 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 covers 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 collisions.
Creating a Document using cURL Utility
You can create a document in CouchDB by sending an HTTP request to the server using PUT method through cURL utility. Following is the syntax to create a document.
$ curl -X PUT http://127.0.0.1:5984/database name/"id" -d '' { document} ''
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 PUT method. When we use the PUT method, the content of the url specifies the object name we are creating using the HTTP request. Here we have to send the following −
-
The name of the database name in which we are creating the document.
-
The document id.
-
The data of the document. −d option is used to send the data/document through HTTP request. While writing a document simply enter your Field-Value pairs separated by colon, within flower brackets as shown below −
{ Name : Raju age : 23 Designation : Designer }
Example
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 −
-
“ok”, indicating the operation was successful.
-
“id”, which stores the id of the document and
-
“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" }
Creating a Document using Futon
To Create a document open the http://127.0.0.1:5984/_utils/ url to get an Overview/index page of CouchDB as shown below.
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.
In this page, you can observe three options − save Document, Add Field and Upload Attachment.
Add Field to the Document
To add field to the document click on Add Field option. After creating a database, you can add a field to it using this option. Clicking on it will get you a pair of text boxes, namely, Field, value. You can edit these values by clicking on them. Edit those values and type your desired Field-Value pair. Click on the green button to save these values.
In the following illustration, we have created three fields Name, age and, Designation of the employee.
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.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí CouchDB – Introduction nhận dự án làm có lương
CouchDB – Introduction
Database management system provides mechanism for storage and retrieval of data. There are three main types of database management systems namely RDBMS (Relational Database management Systems), OLAP (Online Analytical Processing Systems) and NoSQL.
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,
- Horizontal scaling, and
- Finer control over availability.
NoSQL databases use different data structures compared to relational databases. It makes some operations faster in NoSQL. The suitability of a given NoSQL database depends on the problem it must solve. These databases store both structured data and unstructured data like audio files, video files, documents, 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.
Column Store − In these databases, data is stored in cells grouped in columns of data, and these columns are further grouped into Column families. These column families can contain any number of columns.
Examples − BigTable, HBase, and HyperTable.
Document Store − These are the databases developed on the basic idea of key-value 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?
-
CouchDB have an HTTP-based REST API, which helps to communicate with the database easily. And the simple structure of HTTP resources and methods (GET, PUT, DELETE) are easy to understand and use.
-
As we store data in the flexible document-based structure, there is no need to worry about the structure of the data.
-
Users are provided with powerful data mapping, which allows querying, combining, and filtering the information.
-
CouchDB provides easy-to-use replication, using which you can copy, share, and synchronize the data between databases and machines.
Data Model
-
Database is the outermost data structure/container in CouchDB.
-
Each database is a collection of independent documents.
-
Each document maintains its own data and self-contained schema.
-
Document metadata contains revision information, which makes it possible to merge the differences occurred while the databases were disconnected.
-
CouchDB implements multi version concurrency control, to avoid the need to lock the database field during writes.
Features of CouchDB:Reduce the Content
Document Storage
CouchDB is a document storage NoSQL database. It provides the facility of storing documents with unique names, and it also provides an API called RESTful HTTP API for reading and updating (add, edit, delete) database documents.
In CouchDB, documents are the primary unit of data and they also include metadata. Document fields are uniquely named and contain values of varying types (text, number, Boolean, lists, etc.), and there is no set limit to text size or element count.
Document updates (add, edit, delete) follow Atomicity, i.e., they will be saved completely or not saved at all. The database will not have any partially saved or edited documents.
Json Document Structure
{ "field" : "value", "field" : "value", "field" : "value", }
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.
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 hasn’t 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 don’t affect the underlying document, you can have as many different view representations of the same data as you like.
History
- CouchDB was written in Erlang programming language.
- It was started by Damien Katz in 2005.
- CouchDB became an Apache project in 2008.
The current version of CouchDB is 1.61.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc