|
Example
Example code
ExampleSession s = new Session("localhost",8888);
Database db = s.getDatabase("foodb");
Document doc = db.getDocument("documentid1234");
doc.put("foo","bar");
db.save(doc);
Document newdoc = new Document();
doc.put("foo","baz");
db.save(doc); // 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()) {
System.out.println(d.getId());
/*
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 (doc.foo=='bar') { return doc; }}");
|
Sign in to add a comment
This is a very clean and useful interface. I am excited to take it for a test drive.
Grabbing the following jars from SVN is probably the easiest way to satisfy all of the dependencies.
Should "ViewResult?" be "ViewResults?" and db.save() be db.saveDocument() ??
Running this demo against the latest SVN needs those changes to work.
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="http://maven.apache.org/POM/4.0.0"
</project>
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 Database.java class, in the constructor:
now, both couchDB4j and FeatherDB would work nicely :)
Theres a subtle error in the example:
Document newdoc = new Document(); doc.put("foo","baz"); db.save(doc); // auto-generated id given by the database
newdoc is created and never used.The already present doc variable is used altered and saved.