RavenDB 对列表属性进行 Map/Reduce

发布于 2024-11-26 20:27:23 字数 1147 浏览 0 评论 0原文

刚刚学习 Map/Reduce,我错过了一个步骤。我读过这篇文章(使用 .NET 客户端的 RavenDB Map-Reduce 示例 )但无法完全跳转到我需要的内容。

我有一个对象:

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; }
}

我想要做的是获取所有用户的 Map/Reduce 收藏夹属性。像这样的东西(但这显然不起作用):

 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) };

例如,如果用户 1 有收藏夹 1, 2, 3,用户 2 有收藏夹 1,2,那么这应该返回 {{OilId=3, Count =1}, {OilId=2, Count = 2}, {OilId=1,Count = 2}}

当前代码产生异常: System.NotSupportedException : Node not support: Call

我觉得我 关闭。有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-12-03 20:27:23

我编写了一个复制您的代码的小应用程序,但我没有看到抛出的异常。请在此处查看我的代码:http://pastie.org/2308175。输出是

最喜欢的:1,数量:2

最喜欢的:2,数量:2

最喜欢的:3,数量: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.

美人骨 2024-12-03 20:27:23

博尼格,
Map/Reduce 仅适用于跨文档进行聚合。对于这样的事情,通过执行以下操作会得到更好的服务:

  session.Query<User>().Select(u=>u.Favorites).ToList()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文