MongoDB Project Date ranges in mongoDB based on month or Quarter











up vote
0
down vote

favorite












Given a collection, I want to group ISODate by year and month and generate range of dates for each month.



For example if the dates are 10-10-2018, 11-10-2018, 11-10-2017, 04-04-2018
The output should be



{"ranges": [
{"min": 01-10-2018, "max": 31-10-2018},
{"min": 01-10-2017, "max": 31-10-2017},
{"min": 01-04-2018, "max": 31-04-2018},
]}


I could project month and year separately and group them and arrive at



[Oct 2018, Oct 2017....]


Is there a way to arrive at the above output using mongo aggregate query










share|improve this question






















  • docs.mongodb.com/manual/reference/operator/aggregation/…, then docs.mongodb.com/manual/reference/operator/aggregation/bucket if you know ranges or docs.mongodb.com/manual/reference/operator/aggregation/group if you don't.
    – Alex Blex
    Nov 8 at 9:48










  • None of the above. would work. My question is specifically to generate start and end date of a month given a month and year
    – Bhavani Ravi
    Nov 8 at 11:47










  • Use the first link. Start of the month is "%Y-%m-01" End of the month is beginning of next month - 1 day, then "%Y-%m-%d" of the result.
    – Alex Blex
    Nov 8 at 11:54










  • Nice idea thanks. Will try and get back
    – Bhavani Ravi
    Nov 8 at 12:23















up vote
0
down vote

favorite












Given a collection, I want to group ISODate by year and month and generate range of dates for each month.



For example if the dates are 10-10-2018, 11-10-2018, 11-10-2017, 04-04-2018
The output should be



{"ranges": [
{"min": 01-10-2018, "max": 31-10-2018},
{"min": 01-10-2017, "max": 31-10-2017},
{"min": 01-04-2018, "max": 31-04-2018},
]}


I could project month and year separately and group them and arrive at



[Oct 2018, Oct 2017....]


Is there a way to arrive at the above output using mongo aggregate query










share|improve this question






















  • docs.mongodb.com/manual/reference/operator/aggregation/…, then docs.mongodb.com/manual/reference/operator/aggregation/bucket if you know ranges or docs.mongodb.com/manual/reference/operator/aggregation/group if you don't.
    – Alex Blex
    Nov 8 at 9:48










  • None of the above. would work. My question is specifically to generate start and end date of a month given a month and year
    – Bhavani Ravi
    Nov 8 at 11:47










  • Use the first link. Start of the month is "%Y-%m-01" End of the month is beginning of next month - 1 day, then "%Y-%m-%d" of the result.
    – Alex Blex
    Nov 8 at 11:54










  • Nice idea thanks. Will try and get back
    – Bhavani Ravi
    Nov 8 at 12:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Given a collection, I want to group ISODate by year and month and generate range of dates for each month.



For example if the dates are 10-10-2018, 11-10-2018, 11-10-2017, 04-04-2018
The output should be



{"ranges": [
{"min": 01-10-2018, "max": 31-10-2018},
{"min": 01-10-2017, "max": 31-10-2017},
{"min": 01-04-2018, "max": 31-04-2018},
]}


I could project month and year separately and group them and arrive at



[Oct 2018, Oct 2017....]


Is there a way to arrive at the above output using mongo aggregate query










share|improve this question













Given a collection, I want to group ISODate by year and month and generate range of dates for each month.



For example if the dates are 10-10-2018, 11-10-2018, 11-10-2017, 04-04-2018
The output should be



{"ranges": [
{"min": 01-10-2018, "max": 31-10-2018},
{"min": 01-10-2017, "max": 31-10-2017},
{"min": 01-04-2018, "max": 31-04-2018},
]}


I could project month and year separately and group them and arrive at



[Oct 2018, Oct 2017....]


Is there a way to arrive at the above output using mongo aggregate query







mongodb mongodb-query pymongo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 9:26









Bhavani Ravi

682423




682423












  • docs.mongodb.com/manual/reference/operator/aggregation/…, then docs.mongodb.com/manual/reference/operator/aggregation/bucket if you know ranges or docs.mongodb.com/manual/reference/operator/aggregation/group if you don't.
    – Alex Blex
    Nov 8 at 9:48










  • None of the above. would work. My question is specifically to generate start and end date of a month given a month and year
    – Bhavani Ravi
    Nov 8 at 11:47










  • Use the first link. Start of the month is "%Y-%m-01" End of the month is beginning of next month - 1 day, then "%Y-%m-%d" of the result.
    – Alex Blex
    Nov 8 at 11:54










  • Nice idea thanks. Will try and get back
    – Bhavani Ravi
    Nov 8 at 12:23


















  • docs.mongodb.com/manual/reference/operator/aggregation/…, then docs.mongodb.com/manual/reference/operator/aggregation/bucket if you know ranges or docs.mongodb.com/manual/reference/operator/aggregation/group if you don't.
    – Alex Blex
    Nov 8 at 9:48










  • None of the above. would work. My question is specifically to generate start and end date of a month given a month and year
    – Bhavani Ravi
    Nov 8 at 11:47










  • Use the first link. Start of the month is "%Y-%m-01" End of the month is beginning of next month - 1 day, then "%Y-%m-%d" of the result.
    – Alex Blex
    Nov 8 at 11:54










  • Nice idea thanks. Will try and get back
    – Bhavani Ravi
    Nov 8 at 12:23
















docs.mongodb.com/manual/reference/operator/aggregation/…, then docs.mongodb.com/manual/reference/operator/aggregation/bucket if you know ranges or docs.mongodb.com/manual/reference/operator/aggregation/group if you don't.
– Alex Blex
Nov 8 at 9:48




docs.mongodb.com/manual/reference/operator/aggregation/…, then docs.mongodb.com/manual/reference/operator/aggregation/bucket if you know ranges or docs.mongodb.com/manual/reference/operator/aggregation/group if you don't.
– Alex Blex
Nov 8 at 9:48












None of the above. would work. My question is specifically to generate start and end date of a month given a month and year
– Bhavani Ravi
Nov 8 at 11:47




None of the above. would work. My question is specifically to generate start and end date of a month given a month and year
– Bhavani Ravi
Nov 8 at 11:47












Use the first link. Start of the month is "%Y-%m-01" End of the month is beginning of next month - 1 day, then "%Y-%m-%d" of the result.
– Alex Blex
Nov 8 at 11:54




Use the first link. Start of the month is "%Y-%m-01" End of the month is beginning of next month - 1 day, then "%Y-%m-%d" of the result.
– Alex Blex
Nov 8 at 11:54












Nice idea thanks. Will try and get back
– Bhavani Ravi
Nov 8 at 12:23




Nice idea thanks. Will try and get back
– Bhavani Ravi
Nov 8 at 12:23

















active

oldest

votes











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',
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%2f53204790%2fmongodb-project-date-ranges-in-mongodb-based-on-month-or-quarter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53204790%2fmongodb-project-date-ranges-in-mongodb-based-on-month-or-quarter%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini