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 8447591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:13:00+00:00 2026-06-10T10:13:00+00:00

I have a data transformation problem that I’m struggling with: Assume I have a

  • 0

I have a data transformation problem that I’m struggling with:

Assume I have a set of question and answer records from polls loaded into mongo of the form

ID PollTitle Creator Question Answer Respondent

I want to transform these into what I think is a more compact JSON structure:

{
 ID,
 Poll Title,
 Questions : [
    { QuestionTitle, QuestionNumber, Answers : [ 
        { RespondentName, Answer, SubmittedTime }
        ]
    ]
}

this seems like it would be a better way to reduce the number of records and represent the natural hierarchy of Poll -> Questions -> Answers. Here is an example of some records for a single poll:

_id Poll ID Poll Title  Creator Question  Quest#  Responder Answer  Response Time
742888  9258    Daily Checkin   Mike    Was it a good meeting   1   John    Yes  8/16
742889  9258    Daily Checkin   Mike    Was it a good meeting   1   Len No   8/16
742890  9258    Daily Checkin   Mike    Do you agree with goal  2   John    Yes  8/16
742891  9258    Daily Checkin   Mike    Do you agree with goal  2   Len Yes  8/16

struggling to figure out how to do this in the query language using aggregation framework.

  • 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-10T10:13:02+00:00Added an answer on June 10, 2026 at 10:13 am

    You have to do more than one $group step in aggregation framework.

    Here is what it would look like on your sample data and the output you would get:

    db.poll.aggregate(
    [
        {
            "$group" : {
                "_id" : {
                    "ID" : "$ID",
                    "title" : "$title",
                    "QuestionTitle" : "$question",
                    "QuestionNumber" : "$questionNum"
                },
                "answer" : {
                    "$push" : {
                        "responder" : "$responder",
                        "Answer" : "$answer",
                        "respTime" : "$respTime"
                    }
                }
            }
        },
        {
            "$group" : {
                "_id" : {
                    "ID" : "$_id.ID",
                    "title" : "$_id.title"
                },
                "Questions" : {
                    "$push" : {
                        "QuestionTitle" : "$_id.QuestionTitle",
                        "QuestionNumber" : "$_id.questionNumber",
                        "Answers" : "$answer"
                    }
                }
            }
        }
    ])
    {
        "result" : [
            {
                "_id" : {
                    "ID" : 9258,
                    "title" : "Daily Checkin"
                },
                "Questions" : [
                    {
                        "QuestionTitle" : "Do you agree with goal",
                        "Answers" : [
                            {
                                "responder" : "John",
                                "Answer" : "Yes",
                                "respTime" : "8/16"
                            },
                            {
                                "responder" : "Len",
                                "Answer" : "Yes",
                                "respTime" : "8/16"
                            }
                        ]
                    },
                    {
                        "QuestionTitle" : "Was it a good meeting",
                        "Answers" : [
                            {
                                "responder" : "John",
                                "Answer" : "Yes",
                                "respTime" : "8/16"
                            },
                            {
                                "responder" : "Len",
                                "Answer" : "No",
                                "respTime" : "8/16"
                            }
                        ]
                    }
                ]
            }
        ],
        "ok" : 1
    }
    

    You can use $project at the end if you want to rename any of the fields or otherwise transform the exact format of the document.

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

Sidebar

Related Questions

I have data from two different sources that I need to combine. Some data
We have written a number of SSIS packages that import data from CSV files
Here is the problem. I have a stored procedure that transforms normalized data into
I have data from travel diaries which has been read in from a csv
I have data coming from an external system (in CSV form). The data contains
I have data-segment attribute in my body tag that is changed by a slider.
I have data with no relation. Just need to count various columns from 3
I'm working on a problem in C++ that involves lots of subset and transformation
I have an iphone app that uses Core Data to do storage. I have
I have an application, which performs some custom data gathering, transformation and reporting. Currently,

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.