0

I want to create a student database that has details about students - id, name, address, transcript, phone etc. I created a structure like this:

{
 "ssn" : "121-23-1232", 
 "name": "Grace Johnson",
 "address": [
    {"Street Address": "190 Pinehill place", 
    "City": "Belmont",
    "State": "CA",
    "ZipCode": "08250"
    }
    ],
 "phone": "605-123-1111",
 "transcript": [
    {"Math":"B",
    "English":"A",
    "Physics": "B",
    "Psychology": "C",
    "Physical Education": "A"
    }
    ]
 }

However is this method of creation correct if I want to be able to access the students who live in a particular city..How would I write it? Also suppose I want to get all students who receive an A in Physics...

Your help is greatly appreciated.

TheDude
  • 3,796
  • 2
  • 28
  • 51

1 Answers1

0

If you wanted to output all of the students who live in a particular city, your map function can look like this:

function(doc){
  if(doc.address){
    emit([doc.address.city, doc.address.state], doc.name)
  }
}

That will group together all of the students that live in a city. So you can do this GET request to find students who live in a particular city:

GET /<viewUrl?start_key=[Belmont,CA]

The map for grades would be similar:

function(doc){
  if(doc.transcript){
    for(var i = 0; i < doc.transcript.length; i++){
      var classes = Object.keys(doc.transcript[i]);
      for(var j = 0; j < classes.length; j++){
        var class = classes[j];
        emit([class, doc.transcript[j][class]], doc.name)
      }
    }
  }
}

The map will emit all of the classes for the student and their grades. So it query it, you can do:

GET /<viewUrl>?key=[Physics,A]
TheDude
  • 3,796
  • 2
  • 28
  • 51
  • Can you please tell me why when I run the first function that you wrote, i get [null,null] for the key. It doesn't seem to return anything for doc.address.city or doc.address.state. – user3323595 Feb 20 '14 at 22:52
  • I want to get all students who receive an A in Physics - In the solution you gave me where are we checking for the condition that the doc.transcript.Physics='A' ? Can you please let me know how to write that. Can it be included as an if condition in a function(doc). Thanks for your help. – user3323595 Feb 20 '14 at 22:58
  • I added the GET request you can make to get all of the students who got an A in Physics by telling Couch what key you want. – TheDude Feb 20 '14 at 23:02
  • Sorry to sound so ignorant, but this is the first week I am working in Couchdb and I have an assignment to complete. Where do I write the GET command if I am using Futon. – user3323595 Feb 20 '14 at 23:12
  • No worries! I don't think you can specify query parameters in Futon, but you can click on the grey arrow in the right corner of the nav bar on the view page -- it will take you to the URL for that view and you can add the parameters in the address bar. – TheDude Feb 20 '14 at 23:15
  • Since this is for an assignment, I suggest looking at the CouchDb guide on their website to get a better idea on how to use views: http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views – TheDude Feb 20 '14 at 23:16
  • Thank you for your time. I'll go over that and dig around a bit more. Appreciate your help. – user3323595 Feb 20 '14 at 23:25
  • Can you please explain why i am getting this error when trying to do the GET with CURL: curl -X GET http://localhost:5984/students/_design/abc/_view/abc?start_key=[Belmont,CA] curl: (3) [globbing] error: bad range specification after pos 71 – user3323595 Feb 20 '14 at 23:45
  • Actually going down to something even more fundamental. Why would this function give me a null value: function(doc) { if (doc.address) {emit ([doc.address.City); }} – user3323595 Feb 20 '14 at 23:55
  • The syntax is incorrect. It should be `function(doc) { if (doc.address) {emit ([doc.address.City], 1); }}` – TheDude Feb 21 '14 at 00:11
  • It gave me a value of I, against the value column. i want to see the name of the city coming from address. can you confirm that the manner in which i created the document itself will give me the information ? – user3323595 Feb 21 '14 at 00:18