Last Updated: February 25, 2016
· runexec

CouchDB - The Death of Queries & Javscript

This quick tutorial will show you one of many alternatives to the well missed sql query. CouchDB has many different ways of making filters and views, but this one is aimed just for your understanding.

My database is called Coderwall, and we'll get the following JavaScript to act as a query alternative for our view called pro-or-not. This will show us if the tip is from a professional.

function(doc) {
  if(doc.type == "protip" && doc.public) {
    emit("Protip "+doc._id, doc);
  }else{ emit("Not Pro Tip", doc); }

Let's make sure it works


    "_id": "_design/protips",    
    "_rev": "1-ff083d518ab7d3bcda7b111cb3c62f10",    
    "language": "javascript",    
    "views": {    
        "pro-or-not": {    
            "map": "function(doc) {\n if(doc.type == \"protip\" && doc.public) {\n \temit(\"Protip \"+doc._id, doc);\n }else{ emit(\"Not Pro Tip\", doc); }\n}"    


Let's get our view! Notice the value of 'key'

database - coderwall    
_design - protips    
_view - pro-or-not    

"key":"Not Pro Tip",    
"some-doc":"testing it out"}},    

"key":"Protip e9006a913083f861a4e8de607d000fbd",    
"title":"Ryan Kelker Mastering CouchDB"}}