- “FUTON” is a web interface for couch db.
- It can be viewed in “5984” port of the system.
- In the my previous post(link) we have seen how to install and create a database in couch db.
- In another previous post(link) we have seen how to access the content of a database (“student”) in java using couchdb4j library.
- Here we are going to learn a new concept called “VIEW” in couch db.
- Views are used for two purposes in couch db .Those are Querying and Reporting.
- Temporary view and Permanent View are two types of views in couch db.
- In this Demo, “We are going to create a MAP function in Futon.The name of the function say getAllStudentName, It will return the names of student with their id“.
- The Existing “student” database is like(in Futon), Here 4 students are present ,
- Changing the view in futon to temporary view,
- The Temporary view looks like,
- “emit” is the function will returns the value on each document.It takes two argument “key” and “value“.
- Lets write a function in the editor to return the name of students,
/*Retrieves All the Student Names*/
function(document) {
var nameOfStudent;
if (document.name) {
nameOfStudent = [document.name];
emit(document._id,nameOfStudent);
}
}
- The Editor’s Screenshot,
- Now Run This getAllStudentName map function. The output looks like,
- The Object view of the output,
{"total_rows":4,"offset":0,"rows":[
{"id":"326e49164d9b414a6c1ce2c8a802d175","key":"326e49164d9b414a6c1ce2c8a802d175","value":["sandeep"]},
{"id":"326e49164d9b414a6c1ce2c8a802d28e","key":"326e49164d9b414a6c1ce2c8a802d28e","value":["sumanta"]},
{"id":"326e49164d9b414a6c1ce2c8a802dda1","key":"326e49164d9b414a6c1ce2c8a802dda1","value":["sangeeta"]},
{"id":"326e49164d9b414a6c1ce2c8a802f200","key":"326e49164d9b414a6c1ce2c8a802f200","value":["Surabhi"]}
]}