CouchDB学习总结

CouchDB是一种文件存储数据库,一种开源的非关系数据库,CouchDB使用JSON来存储数据,使用JavaScript作为查询语言来转换文档,使用MapReduce和HTTP作为API。
The CouchDB file layout and commitment system features all Atomic Consistent Isolated Durable (ACID) properties. On-disk, CouchDB never overwrites committed data or associated structures, ensuring the database file is always in a consistent state.
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.
Any number of clients can be reading documents without being locked out or interrupted by concurrent updates, even on the same document. CouchDB read operations use a Multi-Version Concurrency Control (MVCC) model where each client sees a consistent snapshot of the database from the beginning to the end of the read operation.
数据结构
Documents are indexed in B-trees by their name (DocID) and a Sequence ID.These B-tree indexes are updated simultaneously when documents are saved or deleted. The index updates always occur at the end of the file (append-only updates).
数据存储
Documents have the advantage of data being already conveniently packaged for storage rather than split out across numerous tables and rows in most database systems. When documents are committed to disk, the document fields and metadata are packed into buffers, sequentially one document after another (helpful later for efficient building of views).
When CouchDB documents are updated, all data and associated indexes are flushed to disk and the transactional commit always leaves the database in a completely consistent state.
Data in CouchDB is stored in semi-structured documents. CouchDB documents are flexible and each has its own implicit structure, which alleviates the most difficult problems and pitfalls of bi-directionally

Curl 默认执行GET操作,使用curl -X POST 或PUT更改操作。-X命令用于记录历史操作记录
To make it easy to work with our terminal history, we usually use the -X option even when issuing GET requests.
Everything is done using GET, PUT, POST, and DELETE with a URI.
查看现有的数据库:
curl -X GET http://127.0.0.1:5984/_all_dbs
创建新的数据库:
curl -X PUT http://127.0.0.1:5984/baseball

删除数据库:
curl -X DELETE http://127.0.0.1:5984/baseball
-v(verbose)命令用于打印进程信息curl -vX DELETE http://127.0.0.1:5984/baseball

使用curl创建文档:
在数据库hello中创建“id”:“10010”的文档,并且写入多个数据,注意这里的格式与网络上的教程有差别,网上教程的格式无法正常写入(正常仅使用双引号,并且在JSON中的双引号前加\)
curl -H “Content-Type: application/json” -X PUT http://localhost:5984/hello/“10010” -d “{\”name\”:\”minsu\”, \”age\”:\”24\” , \”address\” : \”No. 112 ZhongShangRoad GuangZhou\” }”

使用id查看文档的数据:curl http://127.0.0.1:5984/hello/10010

使用curl修改文档:
查看文档当前的版本信息:
C:\Users\zhengxf>curl http://127.0.0.1:5984/hello/10010
{“_id”:”10010”,”_rev”:”2-24c0341a26e2c5bf14d6f7b88ba4ea59”,”age”:”24”}

使用当前的版本信息对文档进行修改:
C:\Users\zhengxf>curl -H “Content-Type: application/json” -X PUT http://localhost:5984/hello/10010 -d “{\”age\”:\”23\” ,\”_rev\”:\”2-24c0341a26e2c5bf14d6f7b88ba4ea59\” }”
修改时需要给出文档的版本号

使用curl删除文档:
C:\Users\zhengxf>curl -H “Content-Type: application/json” -X DELETE http://localhost:5984/hello/001?rev=1-384d2bc91a881c6f9509b974573707bb
同样需要指定删除文档的版本号

你可能感兴趣的:(CouchDB学习总结)