private MonthlyUserStatistic PopulateData(MonthlyUserStatisticData statData)
 {
     MonthlyUserStatistic stat = AutoMapper.Mapper.Map<MonthlyUserStatisticData, MonthlyUserStatistic>(statData);
     return stat;
 }
        private void RecalculateMonth( int month, int year)
        {
            DateTime firstDayOftheMonth = new DateTime(year, month, 1, 0,0,1, DateTimeKind.Utc);
            DateTime lastDayOftheMonth = new DateTime(year, month, 1, 23, 59, 59, DateTimeKind.Utc).AddMonths(1).AddDays(-1);

            var operations = new BsonDocument[]
            {
                new BsonDocument
                            {
                                    {
                                        "$match", new BsonDocument
                                                    {
                                                        {"createdDate", new BsonDocument { {"$gte", firstDayOftheMonth}, {"$lte", lastDayOftheMonth} }}
                                                    }

                                    }
                            },
               new BsonDocument
                            {
                                    {
                                        "$group", new BsonDocument
                                                    {
                                                        { "_id", new BsonDocument
                                                                {
                                                                    {"user", "$user"}
                                                                }
                                                        },
                                                        { "userName", new BsonDocument("$first", "$userName") },
                                                        { "numReviews", new BsonDocument("$sum", 1) },
                                                        { "numLikes", new BsonDocument("$sum", 1) },
                                                        { "Year", new BsonDocument("$first", new BsonDocument (  "$year", "$createdDate" ) )},
                                                        { "Month", new BsonDocument("$first", new BsonDocument (  "$month", "$createdDate" ) )},

                                                    }
                                      }
                            }

               //new BsonDocument("$match", new BsonDocument("createdDate", new BsonDocument ( "$gte", firstDayOftheMonth ))),
               //new BsonDocument("$group", new BsonDocument { { "_id", new BsonDocument("user", "$user") }, { "userName", new BsonDocument("$first", "$userName") }, { "numReviews", new BsonDocument("$sum", 1) }, { "numLikes", new BsonDocument("$sum", 1) }}),
            };

            var operations2 = new BsonDocument[]
            {
                //new BsonDocument("$match", new BsonDocument("createdDate", new BsonDocument ( "$gte", firstDayOftheMonth ))),

                new BsonDocument("$unwind", "$likes"),
                                new BsonDocument
                            {
                                    {
                                        "$match", new BsonDocument
                                                    {
                                                        {"likes.when", new BsonDocument { {"$gte", firstDayOftheMonth}, {"$lte", lastDayOftheMonth} }}
                                                    }

                                    }
                            },
                new BsonDocument("$group", new BsonDocument { { "_id", new BsonDocument("user", "$user") },
                                                        { "userName", new BsonDocument("$first", "$userName") },
                                                        { "numReviews", new BsonDocument("$sum", 1) },
                                                        { "numLikes", new BsonDocument("$sum", 1) },
                                                        { "Year", new BsonDocument("$first", new BsonDocument (  "$year", "$likes.when" ) )},
                                                        { "Month", new BsonDocument("$first", new BsonDocument (  "$month", "$likes.when" ) )},
                }),
            };

            var results = _reviewsRepository.Collection.Aggregate(operations);
            var results2 = _reviewsRepository.Collection.Aggregate(operations2);

            List<MonthlyUserStatisticData> stats = new List<MonthlyUserStatisticData>();
            //drop the table
            //_monthlyStatsRepository.Collection.Drop();

            //for(int i=0; i<= results.ResultDocuments.Count()-1; i++)
            //{
            //    BsonDocument doc = results.ResultDocuments.ElementAt(i);
            foreach (BsonDocument doc in results.ResultDocuments)
            {
                MonthlyUserStatisticData stat = new MonthlyUserStatisticData();

                BsonValue root; // = new BsonObjectId(;
                doc.TryGetValue("_id", out root);
                BsonDocument valreviewuser = root.AsBsonDocument;
                stat.userId = Guid.Parse(valreviewuser.GetValue("user").AsString);

                BsonValue valreviewusername; // = new BsonObjectId(;
                doc.TryGetValue("userName", out valreviewusername);
                stat.userName = valreviewusername.AsString;

                BsonValue valreviewnumreviews; // = new BsonObjectId(;
                doc.TryGetValue("numReviews", out valreviewnumreviews);
                stat.numReviews = valreviewnumreviews.AsInt32;

                BsonValue valreviewmonth; // = new BsonObjectId(;
                doc.TryGetValue("Month", out valreviewmonth);
                int mon = valreviewmonth.AsInt32;
                stat.month = mon;
                switch (mon)
                {
                    case 1:
                        stat.monthName = "Jan";
                        break;
                    case 2:
                        stat.monthName = "Feb";
                        break;
                    case 3:
                        stat.monthName = "Mar";
                        break;
                    case 4:
                        stat.monthName = "April";
                        break;
                    case 5:
                        stat.monthName = "May";
                        break;
                    case 6:
                        stat.monthName = "June";
                        break;
                    case 7:
                        stat.monthName = "July";
                        break;
                    case 8:
                        stat.monthName = "Aug";
                        break;
                    case 9:
                        stat.monthName = "Sept";
                        break;
                    case 10:
                        stat.monthName = "Oct";
                        break;
                    case 11:
                        stat.monthName = "Nov";
                        break;
                    case 12:
                        stat.monthName = "Dec";
                        break;
                }

                BsonValue valreviewyear; // = new BsonObjectId(;
                doc.TryGetValue("Year", out valreviewyear);
                stat.year = valreviewyear.AsInt32;

                stat.lastDayOfMonth = lastDayOftheMonth;
                stat.firstDayOfMonth = firstDayOftheMonth;
                //stat.lastDayOfMonth = d;

                foreach (BsonDocument doc2 in results2.ResultDocuments)
                {
                    BsonValue root2; // = new BsonObjectId(;
                    doc2.TryGetValue("_id", out root2);
                    BsonDocument valreviewuser2 = root2.AsBsonDocument;

                    if (valreviewuser.Equals(valreviewuser2))
                    {
                        BsonValue valreviewnumlikes; // = new BsonObjectId(;
                        doc2.TryGetValue("numLikes", out valreviewnumlikes);
                        stat.numLikes = valreviewnumlikes.AsInt32;
                        break;
                    }
                }
                stat.score = (stat.numReviews + stat.numLikes) * 100;
                stats.Add(stat);

            }
            //now sort them...
            stats.Sort((b, a) => a.score.CompareTo(b.score));

            //now calculate the rankings
            int rankCounter = 1;
            for(int i=0; i<stats.Count(); i++)
            {
                MonthlyUserStatisticData stat = stats.ElementAt(i);
                if (i > 0)
                {

                    MonthlyUserStatisticData prvstat = stats.ElementAt(i - 1);
                    if (prvstat.score > stat.score)
                    {
                        rankCounter++;
                    }
                    stat.rank = rankCounter;
                }
                else
                {
                    stat.rank = rankCounter;
                }
            }
            _monthlyStatsRepository.Collection.InsertBatch(stats);

            //string[] indexes = new string[] {"userId", "month", "year"};
            //IndexKeysBuilder ikb = new IndexKeysBuilder();

            //IndexKeys i1 = ikb.Ascending(new string[] { "userId" });
            //_monthlyStatsRepository.Collection.EnsureIndex();
        }