Couchdb Study Notes

Source: Internet
Author: User
Tags couchdb

Couchdb is an easy-to-use nosql database. Download and install it on the official website, start it, and create a new JS file:

 
VaR HTTP = require ('http'); var Options = {port: 5984, method: 'get', // path: "/_ all_dbs "}; // This callback has only one parameter, namely HTTP. createserver (function (req, Res) {}) var Req = http. request (options, function (RES) {console. log ('status: '+ Res. statuscode); console. log ('headers: '+ JSON. stringify (res. headers); Res. setencoding ('utf8'); var body = "" res. on ('data', function (chunk) {body + = chunk}); Res. once ("end", function () {var JSON = JSON. parse (body); console. log (JSON)}; req. end () req. on ('error', function (e) {console. log ('problem with request: '+ E. message );});

Then open it with node. JS! The console outputs the following message, indicating that the message is successful:

Status: 200 headers: {"server": "couchdb/1.2.0 (Erlang OTP/r14b04)", "date": "Fri, 24 Aug 2:20:53:18 GMT ", "Content-Type": "text/plain; charset = UTF-8", "Content-Length": "40", "cache-control ": "must-revalidate"} {couchdb: 'Welcome ', version: '1. 2.0 '}

Then, modify the options object and check the number of databases in it.

 
VaR Options = {port: 5984, method: 'get', path: "/_ all_dbs "};

Will output an array

 
['_ Replicator', '_ users']

Create a database, which is a put request and the path is the database name.

 
VaR Options = {port: 5984, method: 'put', path: "/AAA "};

Output OK = true indicates success

 
{OK: true}

Note: The same database cannot be created again. If we try to send the above request again, the above request will be returned.

{Error: 'file _ exists', reason: 'The database cocould not be created, the file already exists .'}

To delete a database, use the Delete request. The path is the database name.

 
VaR Options = {port: 5984, method: 'delete', path: "/AAA "};

Insert a record in a database. Because the above AAA is deleted, we will create another albums.

 
VaR Options = {port: 5984, method: 'put', path: "/albums "};

Insert a new record. Most of the records in a nosql database are referred to as documents. It requires a uuid. You just need to create one.

VaR HTTP = require ('http'); // create a database named baseball var Options = {port: 5984, method: 'put', path: "/albums/1"}; var Req = http. request (options, function (RES) {console. log ('status: '+ Res. statuscode); console. log ('headers: '+ JSON. stringify (res. headers); Res. setencoding ('utf8'); var body = "" res. on ('data', function (chunk) {body + = chunk}); Res. once ("end", function () {var JSON = JSON. parse (body); console. log (JSON)}; req. setheader ("Content-Type", "application/JSON") // Add req to the document content. write (JSON. stringify ({"title": "There is nothing left to lose", "artist": "Foo Fighters"}) req. end () req. on ('error', function (e) {console. log ('problem with request: '+ E. message );});

Of course, the uuid created above is too insecure, so you can use couthdb to give your UUID

 
VaR Options = {port: 5984, method: 'get', path: "/_ uuids "};

Let's take out the saved document, that is, the database with id name and get request.

 
VaR Options = {port: 5984, method: 'get', path: "/albums/1 "};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.