1

The row structure in MongoDB is as follows:

{
"_id" : ObjectId("588b0fe98dc6911c54a43be6"),
"GrId" : 1,
"GrTitle" : "GrTitle",
"ServiceType" : 1,
"Url" : "https://twitter.com/xxxxxxxxxx",
"PublishedDate" : ISODate("2017-01-27T09:15:08.000Z"),
"ScrappedDate" : ISODate("2017-01-27T09:16:25.097Z"),
"Keyword" : "keyword",
"ItemId" : "123123",
"SearchType" : 2,
"Content" : "content",
"SocialData" : [ 
    {
        "k" : "Retweets",
        "v" : 0
    }, 
    {
        "k" : "Favorites",
        "v" : 1
    }
  ]
}

I would like to get the values from SocialData, group by PublishedDate. My code at this moment looks like this:

var filter = collection.Aggregate()
            .Match(r => r.PublishedDate <= to)
            .Match(r => r.PublishedDate >= from);

        if (serviceType.HasValue)
            filter = filter.Match(r => r.ServiceType == serviceType);

        if (gameId.HasValue)
            filter = filter.Match(r => r.GrId == gameId.Value);

        return filter
            .Group(
                r => new
                {
                    groupedYear = r.PublishedDate.Year,
                    groupedMonth = r.PublishedDate.Month,
                    groupedDay = r.PublishedDate.Day,
                    itemId = r.ItemId,
                },
                g => new
                {
                    Key = g.Key,
                    RetweetsValue = g.FirstOrDefault().SocialData.Select(s => new KeyValuePair<string, int>(s.Key, s.Value)).FirstOrDefault(k => k.Key == "Retweets").Value
                })
            .Project(
                r => new ChartSummary
                {
                    SocialMedia = r.RetweetsValue,
                    Day = r.Key.groupedDay,
                    Month = r.Key.groupedMonth,
                    Year = r.Key.groupedYear,
                })
            .ToList();

I am getting Exception that FirstOrDefault() is not supported, so how should I retrieve these data?

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
Tomasz Kowalczyk
  • 1,873
  • 2
  • 23
  • 33
  • [This](http://stackoverflow.com/questions/31171451/getting-a-single-object-from-mongodb-in-c-sharp) question might help. –  Jan 30 '17 at 13:12
  • I don't think it is possible, it was a similar question already: http://stackoverflow.com/questions/41902613/what-is-the-c-sharp-equivalent-of-push-and-root-for-mongodb/41903581#41903581 I would get whole dictionary and filter on client side. Or is a SocialMedia really huge dictionary? – Maksim Simkin Jan 30 '17 at 13:18
  • @MaksimSimkin depend on `ServiceType` SocialData is maximum up to 5 elements so it is not huge and keys are unique. – Tomasz Kowalczyk Jan 30 '17 at 13:20

1 Answers1

1

i don't think, MongoDrive could translate it to query. If SocialMedia is not very hige i would get whole dictionary and filter it on client side:

    return filter
            .Group(
                r => new
                {
                    groupedYear = r.PublishedDate.Year,
                    groupedMonth = r.PublishedDate.Month,
                    groupedDay = r.PublishedDate.Day,
                    itemId = r.ItemId,
                },
                g => new
                {
                    Key = g.Key,
                    RetweetsValue = g.First().SocialData
                })
            .Project(
                r => new 
                {
                    SocialMedia = r.RetweetsValue,
                    Day = r.Key.groupedDay,
                    Month = r.Key.groupedMonth,
                    Year = r.Key.groupedYear,
                })
            .ToList()               
            .Select(
            r => new ChartSummary
            {
                SocialMedia = r.SocialMedia.FirstOrDefault(x=>x.Key=="Retweets").Value,
                Day = r.Day,
                Month = r.Month,
                Year = r.Year
            });
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49