You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Rana Bunnni <ra...@yahoo.se> on 2013/09/15 11:29:00 UTC

CouchDB view(difficult issue)

Hi,
I have really and difficult issue.
in my CouchDB , i have two document: student , course
in a student document , i have the following properties(fields):

first_name: jan
last_name: box
type: student,
course_name: java

and in another document :
i have the following fields:

course_name : java
week: 20
type: course
teacher: Max
local: 4012

in my database , i have many students and each student study one course_name in the different week. 
I want make view , that when i search after one student , can  i get all the weeks that he has studied for a specified course? 
how can i do that ? please

Best regards
Rana

Re: CouchDB view(difficult issue)

Posted by Dave Cottlehuber <dc...@jsonified.com>.
Hi Rana

You might need to do some initial work yourself here :-) I've put some
reading notes below to help you get started.

Firstly you don't have enough data to link courses & students by
attendance  -- you'll need to review what data you collect & have
available.

So let's assume you have a doc for each week's attendance by class
(e.g. the teacher collects an attendance record and stores it as a
single doc in couchdb) like this:

{ _id:
  type: "attendance",
  week: "20",
  course_name: "erlang",
  attendees: ["max", "rana", "foo"]
}

You can then iterate over these in a view in a few different ways: in
pseudocode:

for each attendee in attendees
  emit([attendee, course_name], week)
end

This will produce a view that looks like this:

{
    offset: 0,
    rows: [
        {key: ["foo", "erlang"], value: 20},
        {key: ["foo", "erlang"], value: 21},
        {key: ["foo", "erlang"], value: 2},
        {key: ["foo", "erlang"], value: 23},
        {key: ["foo", "javascript"], value: 20},
        {key: ["foo", "javascript"], value: 21},
        {key: ["foo", "javascript"], value: 22},
        {key: ["max", "erlang"], value: 20},
        {key: ["max", "erlang"], value: 21},
        {key: ["max", "erlang"], value: 22},
        {key: ["max", "erlang"], value: 23},
        {key: ["max", "javascript"], value: 20},
        {key: ["max", "javascript"], value: 21},
        {key: ["max", "javascript"], value: 22},

Now, you can use a range query easily to get all keys ["foo",
"erlang"] and use the built in reduce function _count to get a tally
of attended weeks by pupil.

Lots of other ways to order the view & the query but this should get
you started. Note that the view is sorted even though the original
attendees array was not.

You can read up on complex views here [1][2] too for some more ideas,
and included docs here[3]

[1]: http://www.cmlenz.net/archives/2007/10/couchdb-joins
[2]: http://ryankirkman.com/2011/03/30/advanced-filtering-with-couchdb-views.html
[3]: https://couchdb.readthedocs.org/en/latest/api/design.html#multi-document-fetching

On 15 September 2013 11:29, Rana Bunnni <ra...@yahoo.se> wrote:
> Hi,
> I have really and difficult issue.
> in my CouchDB , i have two document: student , course
> in a student document , i have the following properties(fields):
>
> first_name: jan
> last_name: box
> type: student,
> course_name: java
>
> and in another document :
> i have the following fields:
>
> course_name : java
> week: 20
> type: course
> teacher: Max
> local: 4012
>
> in my database , i have many students and each student study one course_name in the different week.
> I want make view , that when i search after one student , can  i get all the weeks that he has studied for a specified course?
> how can i do that ? please
>
> Best regards
> Rana