RavenDB计数总和

时间:2013-02-11 20:41:08

标签: indexing ravendb

我试图在Raven查询中对子集合的计数求和。它返回的计数为0.如果我直接在对象上使用相同的LINQ,那么它的计数为2。

这个查询是否可以在Raven上自动编制索引?如果我需要创建map-reduce索引,有人可以帮我吗?

        [TestMethod]
        public void CalculateUserClickCount()
        {
            var db = new EmbeddableDocumentStore { RunInMemory = true };
            db.Initialize();
            using (var session = db.OpenSession())
            {
                var user = new User();
                var product = new Product();
                product.Clicks.Add(new Click());
                product.Clicks.Add(new Click());
                user.Storefront.EndoProducts.Add(product);
                session.Store(user);
                session.SaveChanges();

                var users = session.Query<User>()
                    .Customize(t => t.WaitForNonStaleResults())
                    .Select(t => new
                    {
                        StoreFrontId = t.Storefront.StorefrontID,
                        itemCount = t.Storefront.EndoProducts.Count,
                        updateDate = t.Storefront.LastUpdateDate,
                        clickCount = t.Storefront.EndoProducts.Sum(r => r.Clicks.Count), // this is improperly set to 0
                        TotalAffiliateRevenue = t.Storefront.SaleReports.Sum(r => r.TotalAffiliateEarnings) // this works
                    })
                    .ToList();

                int clickCount = user.Storefront.EndoProducts.Sum(t => t.Clicks.Count); // this is properly set to 2

                Assert.AreEqual(2, users[0].clickCount);
            }
        }

1 个答案:

答案 0 :(得分:0)

您不需要map / reduce,因为您只能在任何时间对单个文档进行操作。 你需要做的是改变结果,最有可能的是,这样做。