How to bulk import design docs and views to couchbase server

Hello,

I am using couchbase server in a Java project build with Maven and I want to make some kind of import process every time the DB is deleted, so when I make a new deployment in the development environment, the design documents and the preloaded data are existing in the couchbase server instance.

So I have been trying to use the cbdocloader tool following these instructions:

http://docs.couchbase.com/couchbase-manual-2.1/#cbdocloader-tool

In theory, this tool supports data and design documents load. I have tested it for documents and it works but I have tried for design documents but this is not working, I don get any error in the console but in the couchbase console there is no design document, what format should have the json?

Thanks.

Hello,

Yes you can use the cbdocloader tool to import design documents/views.

You just need to create a folder and a file in your import directory/file. something like:

./design_docs/[design_doc_name].json

And in this file:

{
“_id”: “_design/beer”,
“language”: “javascript”,
“views”: {
“brewery_beers”: {
“map”: “function(doc, meta) {\n switch(doc.type) {\n case “brewery”:\n emit([meta.id]);\n break;\n case “beer”:\n if (doc.brewery_id) {\n emit([doc.brewery_id, meta.id]);\n }\n break;\n }\n}\n”
},
“by_location”: {
“map”: “function (doc, meta) {\n if (doc.country, doc.state, doc.city) {\n emit([doc.country, doc.state, doc.city], 1);\n } else if (doc.country, doc.state) {\n emit([doc.country, doc.state], 1);\n } else if (doc.country) {\n emit([doc.country], 1);\n }\n}”,
“reduce”: “_count”
}
}
}

I invite you to look in the beer-sample.zip file.

Note that you can always create views from your code too, as documented here:

Tug
@tgrall

Thank you very much! it works like a charm.

cbdocloader is used for load the ddoc and it will create the views with ddoc. Is my understanding is correct?