Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
How to get value from nested KeyValuePair from IMongoCollection

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
            });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!