Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:31:37+00:00 2026-06-04T04:31:37+00:00

I have an application to track the page visits for a website. Here’s my

  • 0

I have an application to track the page visits for a website.
Here’s my model:

public class VisitSession {
    public string SessionId { get; set; }
    public DateTime StartTime { get; set; }
    public string UniqueVisitorId { get; set; }
    public IList<PageVisit> PageVisits { get; set; }
}

When a visitor go to the website, a visit session starts. One visit session has many page visits. The tracker will write a UniqueVisitorId (GUID) cookie when the first time a visitor go to the website. So we are able to know if a visitor is returning visitor.

Now, I want to know how many unique visitors visited the website in a date range. That is, I want to display a table in our webpage like this;

Date        | Unique Visitors Count
------------+-----------------------
2012-05-01  | 100
2012-05-02  | 1000
2012-05-03  | 120

I want to create an index to do this in RavenDB. But I don’t know how to write the Map/Reduce query. I though it can be like this:

public class UniqueVisitor_ByDate : AbstractIndexCreationTask<VisitSession, UniqueVisitorByDate>
{
    public UniqueVisitor_ByDate()
    {
        Map = sessions => from s in sessions
                            select new
                            {
                                s.StartTime.Date,
                                s.UniqueVisitorId
                            };

        Reduce = results => from result in results
                            group result by result.Date into g
                            select new
                            {
                                Date = g.Key,
                                UniqueVisitorCount = g.Distinct()
                            };
    }
}

But it’s not working. In Ayende’s e-book, I know that the result of Map function should be same as the result of Reduce function. So how can I write the correct map/reduce functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:31:38+00:00Added an answer on June 4, 2026 at 4:31 am

    This index should do what you want:

    public class UniqueVisitor_ByDate : AbstractIndexCreationTask<VisitSession, UniqueVisitorByDate>
    {
        public UniqueVisitor_ByDate()
        {
            Map = sessions => 
                from s in sessions
                select new {
                    s.StartTime.Date,
                    s.UniqueVisitorId,
                    Count = 1,
                };
    
            Reduce = results =>
                from result in results
                group result by result.Date
                into g
                select new UniqueVisitorByDate {
                    Date = g.Key,
                    Count = g.Select(x => x.UniqueVisitorId).Distinct().Count(),
                    UniqueVisitorId = g.FirstOrDefault().UniqueVisitorId,
                };
        }
    }
    

    Note that it requires the extra ‘UniqueVisitorId’ property in the ‘reduce’ and the ‘count’ property in the map, but you can just ignore those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ajax application that uses hashbanging for navigation and I keep track
I have a MVVM application and I want to keep track of the focused
I have Application written with GWT 1.7. I have one page where I upload
I have an application containing tab pages , on one of the tab page
I have a web application where I need to keep track of most popular
I have isolated a hard-to-track bug in a large application to the following minimum
I have a profile page (less than 10k users) and I need to track
I have a web application where I want to keep track of the number
I have a windows application developed in c#.Net which is used as a website
I have a class : @SessionScoped public class LoggedUser { private User user; ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.