Regrouping an Array with a Different key












0















I have a data structure:



[
{
"_id": {
"question": "Voluptatem perferendis voluptas ex.",
"option": "Vero rerum qui animi quia assumenda."
},
"votes_by_gender": [
{
"user_gender": "F",
"total_votes": 1
},
{
"user_gender": "M",
"total_votes": 2
}
]
},
{
"_id": {
"question": "Voluptatem perferendis voluptas ex.",
"option": "Suscipit iste molestias est est porro et atque."
},
"votes_by_gender": [
{
"user_gender": "M",
"total_votes": 2
}
]
}, {...} , ...
]


I need to regroup it. I need a 'question' on the top of the route, and all other the same. So, I'm expeting something like:



[
{ _id : '$question', options : [{option, votes_by_gender},{...} ...] },
....
]


How can I do that?










share|improve this question





























    0















    I have a data structure:



    [
    {
    "_id": {
    "question": "Voluptatem perferendis voluptas ex.",
    "option": "Vero rerum qui animi quia assumenda."
    },
    "votes_by_gender": [
    {
    "user_gender": "F",
    "total_votes": 1
    },
    {
    "user_gender": "M",
    "total_votes": 2
    }
    ]
    },
    {
    "_id": {
    "question": "Voluptatem perferendis voluptas ex.",
    "option": "Suscipit iste molestias est est porro et atque."
    },
    "votes_by_gender": [
    {
    "user_gender": "M",
    "total_votes": 2
    }
    ]
    }, {...} , ...
    ]


    I need to regroup it. I need a 'question' on the top of the route, and all other the same. So, I'm expeting something like:



    [
    { _id : '$question', options : [{option, votes_by_gender},{...} ...] },
    ....
    ]


    How can I do that?










    share|improve this question



























      0












      0








      0








      I have a data structure:



      [
      {
      "_id": {
      "question": "Voluptatem perferendis voluptas ex.",
      "option": "Vero rerum qui animi quia assumenda."
      },
      "votes_by_gender": [
      {
      "user_gender": "F",
      "total_votes": 1
      },
      {
      "user_gender": "M",
      "total_votes": 2
      }
      ]
      },
      {
      "_id": {
      "question": "Voluptatem perferendis voluptas ex.",
      "option": "Suscipit iste molestias est est porro et atque."
      },
      "votes_by_gender": [
      {
      "user_gender": "M",
      "total_votes": 2
      }
      ]
      }, {...} , ...
      ]


      I need to regroup it. I need a 'question' on the top of the route, and all other the same. So, I'm expeting something like:



      [
      { _id : '$question', options : [{option, votes_by_gender},{...} ...] },
      ....
      ]


      How can I do that?










      share|improve this question
















      I have a data structure:



      [
      {
      "_id": {
      "question": "Voluptatem perferendis voluptas ex.",
      "option": "Vero rerum qui animi quia assumenda."
      },
      "votes_by_gender": [
      {
      "user_gender": "F",
      "total_votes": 1
      },
      {
      "user_gender": "M",
      "total_votes": 2
      }
      ]
      },
      {
      "_id": {
      "question": "Voluptatem perferendis voluptas ex.",
      "option": "Suscipit iste molestias est est porro et atque."
      },
      "votes_by_gender": [
      {
      "user_gender": "M",
      "total_votes": 2
      }
      ]
      }, {...} , ...
      ]


      I need to regroup it. I need a 'question' on the top of the route, and all other the same. So, I'm expeting something like:



      [
      { _id : '$question', options : [{option, votes_by_gender},{...} ...] },
      ....
      ]


      How can I do that?







      mongodb mongodb-query aggregation-framework






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 8:10









      Neil Lunn

      97.8k23174184




      97.8k23174184










      asked Nov 22 '18 at 7:57









      Denys SiebovDenys Siebov

      7819




      7819
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to $unwind before you $group:



          db.collection.aggregate([
          { "$unwind": "$votes_by_gender" },
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "F",
          "total_votes" : 1
          }
          },
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          }
          ]
          }


          Of course if you just mean bring the "question" only together without regrouping the array content, then you just $group



          db.collection.aggregate([
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : [
          {
          "user_gender" : "F",
          "total_votes" : 1
          },
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : [
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          }
          ]
          }


          But that is arrays within arrays, and not really recommended even if you thought you wanted that.






          share|improve this answer



















          • 2





            I love you_)))))))))

            – Denys Siebov
            Nov 22 '18 at 8:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426244%2fregrouping-an-array-with-a-different-key%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You need to $unwind before you $group:



          db.collection.aggregate([
          { "$unwind": "$votes_by_gender" },
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "F",
          "total_votes" : 1
          }
          },
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          }
          ]
          }


          Of course if you just mean bring the "question" only together without regrouping the array content, then you just $group



          db.collection.aggregate([
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : [
          {
          "user_gender" : "F",
          "total_votes" : 1
          },
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : [
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          }
          ]
          }


          But that is arrays within arrays, and not really recommended even if you thought you wanted that.






          share|improve this answer



















          • 2





            I love you_)))))))))

            – Denys Siebov
            Nov 22 '18 at 8:40
















          1














          You need to $unwind before you $group:



          db.collection.aggregate([
          { "$unwind": "$votes_by_gender" },
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "F",
          "total_votes" : 1
          }
          },
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          }
          ]
          }


          Of course if you just mean bring the "question" only together without regrouping the array content, then you just $group



          db.collection.aggregate([
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : [
          {
          "user_gender" : "F",
          "total_votes" : 1
          },
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : [
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          }
          ]
          }


          But that is arrays within arrays, and not really recommended even if you thought you wanted that.






          share|improve this answer



















          • 2





            I love you_)))))))))

            – Denys Siebov
            Nov 22 '18 at 8:40














          1












          1








          1







          You need to $unwind before you $group:



          db.collection.aggregate([
          { "$unwind": "$votes_by_gender" },
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "F",
          "total_votes" : 1
          }
          },
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          }
          ]
          }


          Of course if you just mean bring the "question" only together without regrouping the array content, then you just $group



          db.collection.aggregate([
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : [
          {
          "user_gender" : "F",
          "total_votes" : 1
          },
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : [
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          }
          ]
          }


          But that is arrays within arrays, and not really recommended even if you thought you wanted that.






          share|improve this answer













          You need to $unwind before you $group:



          db.collection.aggregate([
          { "$unwind": "$votes_by_gender" },
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "F",
          "total_votes" : 1
          }
          },
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : {
          "user_gender" : "M",
          "total_votes" : 2
          }
          }
          ]
          }


          Of course if you just mean bring the "question" only together without regrouping the array content, then you just $group



          db.collection.aggregate([
          { "$group": {
          "_id": "$_id.question",
          "options": {
          "$push": {
          "option": "$_id.option",
          "votes_by_gender": "$votes_by_gender"
          }
          }
          }}
          ])


          Which would output like:



          {
          "_id" : "Voluptatem perferendis voluptas ex.",
          "options" : [
          {
          "option" : "Vero rerum qui animi quia assumenda.",
          "votes_by_gender" : [
          {
          "user_gender" : "F",
          "total_votes" : 1
          },
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          },
          {
          "option" : "Suscipit iste molestias est est porro et atque.",
          "votes_by_gender" : [
          {
          "user_gender" : "M",
          "total_votes" : 2
          }
          ]
          }
          ]
          }


          But that is arrays within arrays, and not really recommended even if you thought you wanted that.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 8:09









          Neil LunnNeil Lunn

          97.8k23174184




          97.8k23174184








          • 2





            I love you_)))))))))

            – Denys Siebov
            Nov 22 '18 at 8:40














          • 2





            I love you_)))))))))

            – Denys Siebov
            Nov 22 '18 at 8:40








          2




          2





          I love you_)))))))))

          – Denys Siebov
          Nov 22 '18 at 8:40





          I love you_)))))))))

          – Denys Siebov
          Nov 22 '18 at 8:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426244%2fregrouping-an-array-with-a-different-key%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno