Search:

Search all manuals
Search this manual
Manual
Couchbase Server Manual 2.0
Community Wiki and Resources
Download Couchbase Server 2.0
Couchbase Developer Guide 2.0
Client Libraries
Couchbase Server Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
9.7 Design Document REST API
Chapter Sections
Chapters

9.7.1. Storing a Design Document

To create a new design document with one or more views, you can upload the corresponding design document using the REST API with the definition in place. The format of this command is as shown in the table below:

MethodPUT /bucket/_design/design-doc
Request DataDesign document definition (JSON)
Response DataSuccess and stored design document ID
Authentication Requiredoptional
Return Codes 
201Document created successfully.
401The item requested was not available using the supplied authorization, or authorization was not supplied.

Note

When creating a design document through the REST API it is recommended that you create a development (dev) view. It is recommended that you create a dev design document and views first, and then check the output of the configured views in your design document. To create a dev view you must explicitly use the dev_ prefix for the design document name.

For example, using curl, you can create a design document, byfield, by creating a text file (with the name byfield.ddoc) with the design document content using the following command:

shell> curl -X PUT -H 'Content-Type: application/json' \
   http://user:password@localhost:8092/sales/_design/dev_byfield' \
   -d @byfield.ddoc

In the above example:

If successful, the HTTP response code will be 201 (created). The returned JSON will contain the field ok and the ID of the design document created:

JSON
{
    "ok":true,
    "id":"_design/dev_byfield"
}

The design document will be validated before it is created or updated in the system. The validation checks for valid Javascript and for the use of valid built-in reduce functions. Any validation failure is reported as an error.

In the event of an error, the returned JSON will include the field error with a short description, and the field reason with a longer description of the problem.

The format of the design document should include all the views defined in the design document, incorporating both the map and reduce functions for each named view. For example:

JSON
{"views":{"byloc":{"map":"function (doc, meta) {\n  if (meta.type == \"json\") {\n    emit(doc.city, doc.sales);\n  } else {\n    emit([\"blob\"]);\n  }\n}"}}}

Formatted, the design document looks like this:

JSON
{
   "views" : {
      "byloc" : {
         "map" : "function (doc, meta) {\n  if (meta.type == \"json\") {\n    emit(doc.city, doc.sales);\n  } else {\n    emit([\"blob\"]);\n  }\n}"
      }
   }
}

The top-level views field lists one or more view definitions (the byloc view in this example), and for each view, a corresponding map() function.