How to aggregate matching records in a day for a given condition with date and count values stored in an...












0















i am trying to query a mongo collection to get results matching condition "status":new for each date and push the results to an array.



    I have tried the below query, but it is not grouping by date as expected. Please help.


Tried this:




  db.collection.aggregate([
{
$match: {
status: "New"
}
},
{
$sort: {
date: 1
}
},
{
$group: {
_id: {
status: "$status"
},
series: {
$push: {
name: "$Date",
value: {
$sum: 1
}
}
}
}
},
{
$addFields: {
name: "$_id.status"
}
}
])



Sample Collection and expected result:



    **Sample Collection:**



    [
{
"_id": "abcd123",
"Date": "2018-06-08T09:42:36.000Z",
"status": "New",
},
{
"_id": "abcd124",
"Date": "2018-06-09T09:42:36.000Z",
"status": "New"
},
{
"_id": "abcd125",
"Date": "2018-06-09T09:31:44.000Z",
"status": "New"
},
{
"_id": "abcd126",
"Date": "2018-06-10T09:42:43Z",
"status": "New"
},
]



    **Expected Result:**



[
{
"_id": {
"status": "New"
},
"name": "New",
"series": [
{
"name": "2018-06-08T09:42:52Z",
"value": 1
},
{
"name": "2018-06-09T09:42:58Z",
"value": 2
},
{
"name": "2018-06-10T09:42:43Z",
"value": 1
},
]
}
]











share|improve this question



























    0















    i am trying to query a mongo collection to get results matching condition "status":new for each date and push the results to an array.



        I have tried the below query, but it is not grouping by date as expected. Please help.


    Tried this:




      db.collection.aggregate([
    {
    $match: {
    status: "New"
    }
    },
    {
    $sort: {
    date: 1
    }
    },
    {
    $group: {
    _id: {
    status: "$status"
    },
    series: {
    $push: {
    name: "$Date",
    value: {
    $sum: 1
    }
    }
    }
    }
    },
    {
    $addFields: {
    name: "$_id.status"
    }
    }
    ])



    Sample Collection and expected result:



        **Sample Collection:**



        [
    {
    "_id": "abcd123",
    "Date": "2018-06-08T09:42:36.000Z",
    "status": "New",
    },
    {
    "_id": "abcd124",
    "Date": "2018-06-09T09:42:36.000Z",
    "status": "New"
    },
    {
    "_id": "abcd125",
    "Date": "2018-06-09T09:31:44.000Z",
    "status": "New"
    },
    {
    "_id": "abcd126",
    "Date": "2018-06-10T09:42:43Z",
    "status": "New"
    },
    ]



        **Expected Result:**



    [
    {
    "_id": {
    "status": "New"
    },
    "name": "New",
    "series": [
    {
    "name": "2018-06-08T09:42:52Z",
    "value": 1
    },
    {
    "name": "2018-06-09T09:42:58Z",
    "value": 2
    },
    {
    "name": "2018-06-10T09:42:43Z",
    "value": 1
    },
    ]
    }
    ]











    share|improve this question

























      0












      0








      0








      i am trying to query a mongo collection to get results matching condition "status":new for each date and push the results to an array.



          I have tried the below query, but it is not grouping by date as expected. Please help.


      Tried this:




        db.collection.aggregate([
      {
      $match: {
      status: "New"
      }
      },
      {
      $sort: {
      date: 1
      }
      },
      {
      $group: {
      _id: {
      status: "$status"
      },
      series: {
      $push: {
      name: "$Date",
      value: {
      $sum: 1
      }
      }
      }
      }
      },
      {
      $addFields: {
      name: "$_id.status"
      }
      }
      ])



      Sample Collection and expected result:



          **Sample Collection:**



          [
      {
      "_id": "abcd123",
      "Date": "2018-06-08T09:42:36.000Z",
      "status": "New",
      },
      {
      "_id": "abcd124",
      "Date": "2018-06-09T09:42:36.000Z",
      "status": "New"
      },
      {
      "_id": "abcd125",
      "Date": "2018-06-09T09:31:44.000Z",
      "status": "New"
      },
      {
      "_id": "abcd126",
      "Date": "2018-06-10T09:42:43Z",
      "status": "New"
      },
      ]



          **Expected Result:**



      [
      {
      "_id": {
      "status": "New"
      },
      "name": "New",
      "series": [
      {
      "name": "2018-06-08T09:42:52Z",
      "value": 1
      },
      {
      "name": "2018-06-09T09:42:58Z",
      "value": 2
      },
      {
      "name": "2018-06-10T09:42:43Z",
      "value": 1
      },
      ]
      }
      ]











      share|improve this question














      i am trying to query a mongo collection to get results matching condition "status":new for each date and push the results to an array.



          I have tried the below query, but it is not grouping by date as expected. Please help.


      Tried this:




        db.collection.aggregate([
      {
      $match: {
      status: "New"
      }
      },
      {
      $sort: {
      date: 1
      }
      },
      {
      $group: {
      _id: {
      status: "$status"
      },
      series: {
      $push: {
      name: "$Date",
      value: {
      $sum: 1
      }
      }
      }
      }
      },
      {
      $addFields: {
      name: "$_id.status"
      }
      }
      ])



      Sample Collection and expected result:



          **Sample Collection:**



          [
      {
      "_id": "abcd123",
      "Date": "2018-06-08T09:42:36.000Z",
      "status": "New",
      },
      {
      "_id": "abcd124",
      "Date": "2018-06-09T09:42:36.000Z",
      "status": "New"
      },
      {
      "_id": "abcd125",
      "Date": "2018-06-09T09:31:44.000Z",
      "status": "New"
      },
      {
      "_id": "abcd126",
      "Date": "2018-06-10T09:42:43Z",
      "status": "New"
      },
      ]



          **Expected Result:**



      [
      {
      "_id": {
      "status": "New"
      },
      "name": "New",
      "series": [
      {
      "name": "2018-06-08T09:42:52Z",
      "value": 1
      },
      {
      "name": "2018-06-09T09:42:58Z",
      "value": 2
      },
      {
      "name": "2018-06-10T09:42:43Z",
      "value": 1
      },
      ]
      }
      ]








      mongodb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 9:43









      StackDStackD

      53




      53
























          1 Answer
          1






          active

          oldest

          votes


















          0














          @matthPen answered this earlier. Here: How to aggregate the sum for each distinct record on a given condition and get the result in an array in mongodb



          I just had to add "addFields" to his answer. My mistake to not check his answer before asking it here.



          db.collection.aggregate([
          {
          $match: {
          status: "New"
          }
          },
          {
          $group: {
          _id: "$Date",
          value: {
          $sum: 1
          }
          }
          },
          {
          $group: {
          _id: null,
          series: {
          $push: {
          name: "$_id",
          value: "$value"
          }
          }
          }
          },
          {
          $addFields: {
          name: "New"
          }
          },

          ])


          Might help someone looking for similar query






          share|improve this answer

























            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%2f53316502%2fhow-to-aggregate-matching-records-in-a-day-for-a-given-condition-with-date-and-c%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









            0














            @matthPen answered this earlier. Here: How to aggregate the sum for each distinct record on a given condition and get the result in an array in mongodb



            I just had to add "addFields" to his answer. My mistake to not check his answer before asking it here.



            db.collection.aggregate([
            {
            $match: {
            status: "New"
            }
            },
            {
            $group: {
            _id: "$Date",
            value: {
            $sum: 1
            }
            }
            },
            {
            $group: {
            _id: null,
            series: {
            $push: {
            name: "$_id",
            value: "$value"
            }
            }
            }
            },
            {
            $addFields: {
            name: "New"
            }
            },

            ])


            Might help someone looking for similar query






            share|improve this answer






























              0














              @matthPen answered this earlier. Here: How to aggregate the sum for each distinct record on a given condition and get the result in an array in mongodb



              I just had to add "addFields" to his answer. My mistake to not check his answer before asking it here.



              db.collection.aggregate([
              {
              $match: {
              status: "New"
              }
              },
              {
              $group: {
              _id: "$Date",
              value: {
              $sum: 1
              }
              }
              },
              {
              $group: {
              _id: null,
              series: {
              $push: {
              name: "$_id",
              value: "$value"
              }
              }
              }
              },
              {
              $addFields: {
              name: "New"
              }
              },

              ])


              Might help someone looking for similar query






              share|improve this answer




























                0












                0








                0







                @matthPen answered this earlier. Here: How to aggregate the sum for each distinct record on a given condition and get the result in an array in mongodb



                I just had to add "addFields" to his answer. My mistake to not check his answer before asking it here.



                db.collection.aggregate([
                {
                $match: {
                status: "New"
                }
                },
                {
                $group: {
                _id: "$Date",
                value: {
                $sum: 1
                }
                }
                },
                {
                $group: {
                _id: null,
                series: {
                $push: {
                name: "$_id",
                value: "$value"
                }
                }
                }
                },
                {
                $addFields: {
                name: "New"
                }
                },

                ])


                Might help someone looking for similar query






                share|improve this answer















                @matthPen answered this earlier. Here: How to aggregate the sum for each distinct record on a given condition and get the result in an array in mongodb



                I just had to add "addFields" to his answer. My mistake to not check his answer before asking it here.



                db.collection.aggregate([
                {
                $match: {
                status: "New"
                }
                },
                {
                $group: {
                _id: "$Date",
                value: {
                $sum: 1
                }
                }
                },
                {
                $group: {
                _id: null,
                series: {
                $push: {
                name: "$_id",
                value: "$value"
                }
                }
                }
                },
                {
                $addFields: {
                name: "New"
                }
                },

                ])


                Might help someone looking for similar query







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 20 '18 at 7:45

























                answered Nov 20 '18 at 7:32









                StackDStackD

                53




                53
































                    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%2f53316502%2fhow-to-aggregate-matching-records-in-a-day-for-a-given-condition-with-date-and-c%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

                    Bressuire

                    Vorschmack

                    Quarantine