0
votes

I have the following document in Couch DB and I want to write a query to retrieve all students who's program = "MSc". How do I do that?

Document

{
  "_id": "students",
  "_rev": "10-f1c709ee8be06faaf7065e785dd72f2b",
  "Students": [
    {
      "StudentId": "2015156",
      "Name": "Trinity",
      "Age": "32",
      "Program": "MSc"
    },
    {
      "StudentId": "2015157",
      "Name": "Thomas Anderson",
      "Age": "34",
      "Program": "MSc"
    }
  ]
}
1
You can either have a map function that will emit all the values in the sub-array. Or, you can use the Mango Query index(if you are using CouchDB 2.0) and use the $elemMatch operator. - Alexis Côté
With a Mango query, I don't see how to filter only the array elements that have "MSc". Since it's all in one single doc and the "fields" mechanism doesn't allow for testing value. Would love to be proven wrong though ;) - zlr
Mango Query can be used to filter documents. I guess you cannot filter contents inside a document using query. - Vivek Pandita

1 Answers

0
votes

Write a view that emits the program as the key, and the student block/hash/object as the value.