Chinaunix首页 | 论坛 | 博客
  • 博客访问: 509705
  • 博文数量: 101
  • 博客积分: 1635
  • 博客等级: 上尉
  • 技术积分: 1282
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-05 01:51
文章分类

全部博文(101)

文章存档

2019年(2)

2018年(16)

2013年(14)

2012年(69)

我的朋友

分类: 数据库开发技术

2012-11-30 18:01:54

Step 1 – Installing CouchDB

The easiest way to get CouchDB up and running on your system is to head to  and download a CouchDB distribution for your OS — OSX in my case. Download the zip, extract it and drop CouchDBX in my applications folder (instructions for other OS’s on CouchOne).

Finally, open CouchDBX.

Step 2 – Welcome to Futon

After CouchDB has started, you should see the Futon control panel in the CouchDBX application. In case you can’t, you can access Futon via your browser. Looking at the log, CouchDBX tells us CouchDB was started at  (may be different on your system). Open a browser and go to_utils/ and you should see Futon.

Throughout the rest of this tutorial I will be using Futon in Firefox. I’ll also have Firebug and the console view open to see all the HTTP requests Futon is sending behind the scenes. This is useful as your application can do everything Futon is doing. Let’s go ahead and create a database called mycouchshop.

Step 3 – Users in CouchDB

CouchDB, by default, is completely open, giving every user admin rights to the instance and all its databases. This is great for development but obviously bad for production. Let’s go ahead and setup an admin. In the bottom right, you will see “Welcome to Admin Party! Everyone is admin! Fix this”.

Go ahead and click fix this and give yourself a username and password. This creates an admin account and gives anonymous users access to read and write operations on all the databases, but no configuration privileges.

Step 4 – Creating a Product Document

Now let’s create our first document using Futon through the following steps:

  1. Open the mycouchshop database.
  2. Click “New Document”.
  3. Click “Add Field” to begin adding data to the JSON document. Notice how an ID is pre-filled out for you, I would highly advise not changing this. Add key “name” with the value of “Nettuts CouchDB Tutorial One”.
  4. Make sure you click the tick next to each attribute to save it.
  5. Click “Save Document”.

Go up a level, back to the database and you should see one document listed with the previous ID as the key and a value beginning with{rev: . This is the JSON document you just created.

Step 5 – Updating a Document

CouchDB is an append only database — new updates are appended to the database and do not overwrite the old version. Each new update to a JSON document with a pre-existing ID will add a new revision. This is what the automatically inserted revision key signifies. Follow the steps below to see this in action:

  • Viewing the contents of the mycouchshop database, click the only record visible.
  • Add another attribute with the key “type” and the value “product”.
  • Hit “Save Document”.

After hitting save, a new revision key should be visible starting with the number 2. Going back a level to themycouchshop database view, you will still see just one document, this is the latest revision of our product document.

Step 6 – Creating a Document Using cURL

I’ve already mentioned that CouchDB uses a RESTful interface and the eagle eyed reader would have noticed Futon using this via the console in Firebug. In case you didn’t, let’s prove this by inserting a document using cURL via the Terminal.

First, let’s create a JSON document with the below contents and save it to the desktop calling the fileperson.json.

  1. {  
  2.     "forename""Gavin",  
  3.     "surname":  "Cooper",  
  4.     "type":     "person"  
  5. }  

Next, open the terminal and execute cd ~/Desktop/ putting you in the correct directory and then perform the insert with curl -X POST mycouchshop/ -d @person.json -H "Content-Type: application/json". CouchDB should have returned a JSON document similar to the one below.

  1. {"ok":true,"id":"c6e2f3d7f8d0c91ce7938e9c0800131c","rev":"1-abadd48a09c270047658dbc38dc8a892"}  

This is the ID and revision number of the inserted document. CouchDB follows the RESTful convention and thus:

  • POST – creates a new record
  • GET – reads records
  • PUT – updates a record
  • DELETE – deletes a record
阅读(1928) | 评论(0) | 转发(0) |
0

上一篇:ftp同步服务器数据

下一篇:oracle dsi文档

给主人留下些什么吧!~~