Reputation: 1355
Lets say i have collection of users. They have "subjects" and each subject has priority -- which determines what priority has user in this subject. I had a few situations like this, but for now let it be users, subjects and priorities.
I want to display users who have selected subject and sort them by priority of this subject. Users and subjetcs are in "many to many" realtions and this is the sample user structure:
{ name:"Nic",
subjects: [
{ name:"math", priority:10},
{ name:"english", priority: 11} ...]}
How I should organize the view structure?
First variant is to create view by-subject
, reduce the users by subject key and get the list of "math" users like _view/by-subjects?key="math"&reduce=true
. But this case I have to sort by priority in client application...
Second is to create separate view for each subject and emit priority
as a key. So i could take the data from _view/math?descending=true
and it would be sorted. But as I understood each time when new user inserted in DB all the 42 views will be updated (in case I have 42 subjects). In first case I have only one view to be updated.
So, the question is: which variant is more preferable for CouchDB, or more idiomatic?
Upvotes: 1
Views: 61
Reputation: 1225
If you emit
[":subject",":user",":priority"]
as key and request ?key="math"
you will get an alphabetical sorted list of user and their priorities.
If you emit
[":subject",":priority",":user"]
You get the same list sorted by priority
. The you can also ask for the top-ten by addin ?limit=10
Upvotes: 2