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