Map Reduce Couch Db

843 Views Asked by At

I have a simple database with couch db. I have users have that fields:

String userName
String password
String mail
boolean admin

I keep my users at db. However I am so new to Couch DB and NoSql. How can I implement map reduce on it with an example (or does it do it internally and I don't need to do anything?)

I use Spring 3 and Ekorp for my application.

2

There are 2 best solutions below

0
On BEST ANSWER

in couch, add a field in your documents called

_type

or

type_

The map function could be

function(doc) {
 if(doc.type_=="user") { 
   emit(doc.name,doc._id);
 }
}
0
On

Your question could use a little more detail, but as I understand it, you want to create a Couch DB document that acts like "table" of users. To do that, you will need to create a JSON document on your Couch DB with the following properties:

{
    type: "user",
    username: "John",
    password: "*****",
    mail: "blah, blah, blah",
    admin: true
}

Just as lukecampbell had pointed out above, you will need to create your own "type" property to identify each entity... For example, rather than using an admin property to identify users with privileges , you could just change the "type" property:

{
    type: "admin"...
}

To query the database, and retrieve specific documents, you'll need to use a map function. Again lukecampbell's example provides a good starting point:

function(doc){
    if(doc.type == "user"){ 
        emit(doc.name, doc);
    }
}

This 'query' will give you a "view" of every "user" entity document, ordered by "doc.name". Here's where the beauty of Couch DB becomes apparent... Map (and reduce) functions are like SQL queries in a certain sense... But because they are written in JavaScript, you can use JavaScript logic to shape your database:

function(doc){
     if(doc.type == "user" && doc.name == "John"){ 
         emit(doc.name, doc);
     }
}

This will give you a view of every "user" document where the user's name is John. As you can see, in some cases, writing queries in this way can be far more expressive than SQL... It's even possible to "duck" type document fields in this way;

function(doc){
    if(
        doc.type == "user" && 
        typeof doc.admin == "boolean"
    ){ 
        emit(doc.name, "Is Admin?: " + JSON.stringify(doc.admin);
    }
}

This "query" should print out the name of each "user" and a little status message ("Is Admin?: true (or false)") for every user document with a boolean value for the admin property. As for "Reduce" functions, they are extremely useful, but it's much more important to understand map functions fully first, because they are VERY versatile, and in most cases a well written map function will obviate the need to write a "reduce" function...

There's LOADS of potential in Couch DB. This site is probably the best place to start; http://guide.couchdb.org/index.html - That said, there are also some very strange quirks in the technology that will occasionally force you to rethink your database designs... One of those quirks are discussed briefly here (see Victor Nicollet's answer):

Is there anything wrong with creating Couch DB views with null values?