Can couchdb do loops?
Let's say I have a database of interests that have 3 fields subject1,subject2,subject3. example, cats,nutrition,hair or space,telescopes,optics etc.
A person (A) has 10 interests composed of 3 fields each.
10 more people B,C,D...have 10 interests each composed of 3 subjects each.
When person A logs in I want the system to search for all people with matching interests.
In javascript I would normally loop through all the interests and then find matching ones I guess using two loops. Then store the matches in another database for the user like "matchinginterests".
Is there any easy way to do this in couchdb compared to mysql -- which seems very complicated.
Thanks, Dan
Is that what you wanted to do?
If you design your tables a little smarter though you'd do it like
Using the following tables
This is how (what you call) 'looping' in SQL works...
First you take person with name 'A' from the table.
You look up all the interests
Then you match everyone elses interests
And then you match the person to them's interest (except for me of course)
Then you return only the data from
them
and 'throw' the rest away, and remove duplicate rowsDISTINCT
.Hope this helps.
I think I understand what you are asking. The answer is pretty straightforward with Map/Reduce.
Say you have the following people documents:
You would probably want to emit your key as the interest, with the value as the person's name (or
_id
).Your view results would look like this:
To get a list of people with computers as an interest, you can simply send
key="computers"
as part of the query string.If you want to add a reduce function to your map, you can simply use
_count
(shortcut to use a compiled reduce function) and you can retrieve a count of all the people with a particular interest, you can even use that to limit which interests you query to build your relationships.