Jurg,
In your case I think the traditional bit would be that you need enough
information in your comment docs to determine what they represent.
Ie, for your comments you have two types of docs:
{"_id": "foo", "_rev": "asdfasd", "article_id": "bar", "type": "comment"}
{"_id": "baz", "_rev": "asjfasl;djf", "file_id": "biz", "type": "comment"}
then your maps do something like:
if(doc.type == "comment" && doc.article_id != undefined) // is article
comment
etc etc.
HTH,
Paul Davis
Jurg van Vliet wrote:
hi luke,
thanks for your reply :)
i have comments for different types of documents, that is what makes
it complicated. i have comments for files and comments for articles.
if i map my documents of type Article and Comment to be reduced the
way below i get all articles, and all comments. also the comments for
files.
the way i see how this could be implemented is to give the comment a
type matching the type of its parent. the comment type would be
ArticleComment. then something like the code we talk about would work.
working this through i start to understand map/reduce better and
better. but i feel a different way of modeling is required to
construct aggregated documents reflecting these relationships.
i guess we need to find a different way to store the information in
our documents. the way we choose is to enable simple nested comments.
(we wanted a view that returned the comments nested, or at least in
order of presentation. we want couchdb to work for us, not the app.)
:) so, what paul and you suggest does not work. it would work
perfectly fine in a database with every Comment related to an Article.
but we also have Comments related to Files.
groet,
jurg.
On Apr 29, 2009, at 7:43 PM, Luke Randall wrote:
Jurg van Vliet wrote:
if i don't emit anything in the last emit statement i get comments
for all of my document types. then case i end up with a list of
articles and comments, both with a comment count.
Hi Jurg
From what I understand of your situation, I don't see why what Paul
suggested wouldn't work. If you have the following as your map
function
function(doc) {
if( doc.type == 'Article') {
emit( doc._id, 0);
} else if( doc.type == 'Comment') {
emit( doc.path[0], 1);
}
}
when you reduce it* you will have a list of articles with a comment
count. Note that you there is no requirement to call emit for each
document. If you don't want a document to appear in a view then just
don't call emit on it. Reduce isn't meant to be used as a filter,
that's what map is for.
* Remember to add :group => true to your couchrest view call, as Futon
silently adds it, but it otherwise defaults to being false.
Sorry, this is basically a rehash of what Paul said, but it seems that
you possibly misunderstood his suggestion. Either that or I
misunderstood your reply, which is also quite possible... :)
- Luke
PS Sorry if this message breaks threading; I wasn't subscribed to the
list, so I had to copy and paste this.