My favorites | Sign in
Project Home Downloads Wiki Issues Source
Example code
Featured, Phase-Implementation
Updated Feb 4, 2010 by


Session s = new Session("localhost",8888);
Database db = s.getDatabase("foodb");

Document doc = db.getDocument("documentid1234");

Document newdoc = new Document();
doc.put("foo","baz");; // auto-generated id given by the database

// Running a view

ViewResult result = db.getAllDocuments(); // same as db.view("_all_dbs");
for (Document d: result.getResults()) {

		ViewResults don't actually contain the full document, only what the view
		returned.  So, in order to get the full document, you need to request a
		new copy from the database.
	*/	Document full = db.getDocument(d.getId());

// Ad-Hoc view

ViewResult resultAdHoc = db.adhoc("function (doc) { if ('bar') { return doc; }}");

Comment by, Dec 3, 2007

This is a very clean and useful interface. I am excited to take it for a test drive.

Comment by, Dec 3, 2007

Grabbing the following jars from SVN is probably the easiest way to satisfy all of the dependencies.

  • lib/test/junit-4.1.jar
  • lib/test/easymock.jar
  • lib/test/log4j.jar
  • lib/commons-codec-1.3.jar
  • lib/commons-httpclient-3.1.jar
  • lib/json-lib-2.0-jdk15.jar
  • lib/commons-collections.jar
  • lib/commons-lang.jar
  • lib/commons-logging-1.1.jar
  • lib/commons-beanutils.jar
  • lib/ezmorph-1.0.3.jar
Comment by, Sep 8, 2008

Should "ViewResult?" be "ViewResults?" and be db.saveDocument() ??

Running this demo against the latest SVN needs those changes to work.

Comment by, Dec 30, 2008

If you use Maven, here's the pom file that will get you going. Save it along with the jar as as %REPO%/com/fourspaces/couchdb4j/couchdb4j/0.1.2/couchdb4j-0.1.2.pom

<?xml version="1.0"?>

<project xmlns=""

xmlns:xsi="" xsi:schemaLocation="">

<modelVersion>4.0.0</modelVersion> <groupId>com.fourspaces</groupId> <artifactId>couchdb4j</artifactId> <version>0.1.2</version> <packaging>jar</packaging> <name>CouchDB4j</name>
Java API for accessing the REST interface of CouchDB (or FeatherDB)
<groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version>
</dependency> <dependency>
<groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version>
</dependency> <dependency>
<groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.3</version>
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version>
</dependency> <dependency>
<groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version>
</dependency> <dependency>
<groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1</version>
</dependency> <dependency>
<groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.2</version> <classifier>jdk15</classifier>


Comment by, Dec 30, 2008

BTW, Just tried it with featherDB instead of couchDB. Apparently FeatherDB do not have update_seq as a DB attribute, and thus session.getDatabase() will throw an exception. You can hack it by modifying the couchdb4j class, in the constructor:

if (json.containsKey("update_seq")) {
updateSeq = json.getInt("update_seq");
} else {
updateSeq = 0;

now, both couchDB4j and FeatherDB would work nicely :)

Comment by, Mar 7, 2009

Theres a subtle error in the example:

Document newdoc = new Document(); doc.put("foo","baz");; // auto-generated id given by the database

newdoc is created and never used.The already present doc variable is used altered and saved.

Comment by, Jul 9, 2009

@oyvind: agreed.

It would also be really nice to flesh out this example a bit more and add:

1) non-simple values (e.g. a real JSON object, not just a string) 2) attachments

as neither are obvious.

Comment by, Nov 19, 2012

hi, I face a exception at Session dbSession = new Session("localhost", 5984); when i try to run the program

Exception in thread "main" java.lang.NoClassDefFoundError?: org/apache/commons/httpclient/HttpException?

at Main.main(
Caused by: java.lang.ClassNotFoundException?: org.apache.commons.httpclient.HttpException?
at$ at Method) at at java.lang.ClassLoader?.loadClass(ClassLoader?.java:306) at sun.misc.Launcher$AppClassLoader?.loadClass( at java.lang.ClassLoader?.loadClass(ClassLoader?.java:247) ... 1 more

i have used the following jars commons-codec-1.6.jar commons-logging-1.1.1.jar couchdb4j-0.1.2.jar fluent-hc-4.2.2.jar gson-2.2.2.jar httpclient-4.2.2.jar httpclient-cache-4.2.2.jar httpcore-4.2.2.jar httpmime-4.2.2.jar

Comment by, Feb 18, 2013

How to get attachment from document using db.getAttachement. the attachment asking two parameters such as id and attachment body, i could not understand what is attachment body and what is attachment body value

Sign in to add a comment
Powered by Google Project Hosting