Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB Map/Reduce over property that is a list

Just learning Map/Reduce and I'm missing a step. I've read this post ( RavenDB Map-Reduce Example using .NET Client ) but can't quite make the jump to what I need.

I have an object:

public class User : IIdentifiable
{
    public User(string username)
    {
        Id = String.Format(@"users/{0}", username);
        Favorites = new List<string>();
    }

    public IList<string> Favorites { get; protected set; }

    public string Id { get; set; }
}

What I want to do is get Map/Reduce the Favorites property across all Users. Something like this (but this obviously doesn't work):

 Map = users => from user in users
                from oil in user.Favorites
                select new { OilId = oil, Count = 1 };
 Reduce = results => from result in results
                     group result by result.OilId into g
                     select new { OilId = g.Key, Count = g.Sum(x => x.Count) };

For example, if User1 has favorites 1, 2, 3 and User 2 has favorites 1,2, then this should return back {{OilId=3, Count =1}, {OilId=2, Count = 2}, {OilId=1,Count = 2}}

The current code produces an exception: System.NotSupportedException : Node not supported: Call

I feel like I'm close. Any help?

like image 875
Matthew Bonig Avatar asked Nov 25 '25 23:11

Matthew Bonig


2 Answers

I wrote a small application replicating your code, but I don't see the exception thrown. See my code here: http://pastie.org/2308175. The output is

Favorite: 1, Count: 2

Favorite: 2, Count: 2

Favorite: 3, Count: 1

which is what I would expect.

like image 197
Thomas Freudenberg Avatar answered Nov 27 '25 13:11

Thomas Freudenberg


MBonig, Map/Reduce is only useful for doing aggregation across documents. For something like this, you would be served far better by doing something like:

  session.Query<User>().Select(u=>u.Favorites).ToList()
like image 34
Ayende Rahien Avatar answered Nov 27 '25 12:11

Ayende Rahien