Simple map-reduce question

17 views
Skip to first unread message

Justin A

unread,
Jun 18, 2018, 11:09:09 AM6/18/18
to RavenDB - 2nd generation document database
Hi all,

got a simple map-reduce question. I'm sure I'm not looking at the problem, right.

Currently, I have a map-reduce index which is trying to do the following: For each USER in the system, return the USER + total number of Foo's they have (i.e. a User Summary).

User is a document.
public class User
{
   string Id;
   string Name;
}

Foo is a document, like:

public class Foo
{
  string Id;
  string UserId; // e.g. users/1-A
}

For reasons, they are separate documents as such.

Now, if the user has no Foo's, I get a NULL back because my index is sorta this:

public Foo_GetUserSummary()
{
    AddMapForAll<Foo>(foos => from f in foos
                              let user = LoadDocument<User>(f.UserId)
                              ...
                              select new 
                              {
                                  f.UserId,
                                  ..
                                  FooCount = 1
                              });
                              
    Reduce = results => from r in results
                        group r by new { f.UserId, ... }
                        into g
                        select new
                        {
                            g.key.UserId,
                            ....
                            FooCount = g.Sum(x => x.FooCount)
                        };
}

So i'm guessing that, because User #1 has no Foo's, then user #1 is not part of the result set.

Any thoughts on how I should tackle this problem, please?

cheers!

-PK-

Grisha Kotler

unread,
Jun 18, 2018, 11:43:08 AM6/18/18
to rav...@googlegroups.com
Add another map on the users collection (set the FooCount to 0)

AddMapForAll<User>(users => from u in users
                              
                              ...
                              select new 
                              {
                                  u.Id,
                                  ..
                                  FooCount = 0
                              });


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin A

unread,
Jun 18, 2018, 9:07:25 PM6/18/18
to RavenDB - 2nd generation document database
Ah of course, I still forget that you can add multiple types in here, assuming the result set are all in the same format, etc.

Awesome! ta!
Reply all
Reply to author
Forward
0 new messages