RavenDB Map/Reduce over property that is a list

笑着哭i 提交于 2019-12-12 12:43:47

问题


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?


回答1:


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.




回答2:


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()


来源:https://stackoverflow.com/questions/6853754/ravendb-map-reduce-over-property-that-is-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!