Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

mongoose - I want to return sum of values by month from JAN-DEC in a year in mongodb

I want to write a Mongoose query to get the sum total for every month (Jan-Dec) in a year.

Currently the query I wrote only returns a value of the sum total for a month and skips all other months. I want a situation where all other months with no sum total should just return 0

Here is my query

db.getCollection('sales_records').aggregate([
{ $match: { createdDate: { $gt: ISODate('2020-01-01 00:00:00.000Z'), $lt: ISODate('2020-12-25 00:00:00.000Z')} } },
{ $match: { merchantId: 5547 } },
  {
    $group : {
      _id: {
          
        year: { $year : "$createdDate" },
        month: { $month : "$createdDate" },
        //week: { $week : "$createdDate" },
      // day: { $day : { $week : "$createdDate" } },
        //hour: { $hour : "$createdDate" },
      },
      merchantId: { $first:  "$merchantId"  },
      date: { $first:  "$createdDate"  },
      total: { $sum : "$totalAmount"}
    }
  },
  {
    $sort: { date: -1 }
  }
])



{
    "_id" : {
        "year" : 2020,
        "month" : 4
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-04-01T00:00:00.000Z"),
    "total" : 2100.0
}

/* 2 */
{
    "_id" : {
        "year" : 2020,
        "month" : 3
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-03-02T09:34:23.870Z"),
    "total" : 396511341.0
}

But I wanted to return something like this

{
    "_id" : {
        "year" : 2020,
        "month" : 6
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-04-01T00:00:00.000Z"),
    "total" : 0
}

{
    "_id" : {
        "year" : 2020,
        "month" : 5
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-04-01T00:00:00.000Z"),
    "total" : 0
}

{
    "_id" : {
        "year" : 2020,
        "month" : 4
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-04-01T00:00:00.000Z"),
    "total" : 2100.0
}

{
    "_id" : {
        "year" : 2020,
        "month" : 3
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-03-02T09:34:23.870Z"),
    "total" : 396511341.0
}
{
    "_id" : {
        "year" : 2020,
        "month" : 2
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-03-02T09:34:23.870Z"),
    "total" : 0
}
{
    "_id" : {
        "year" : 2020,
        "month" : 1
    },
    "merchantId" : NumberLong(5547),
    "date" : ISODate("2020-03-02T09:34:23.870Z"),
    "total" : 0
}
question from:https://stackoverflow.com/questions/65870238/i-want-to-return-sum-of-values-by-month-from-jan-dec-in-a-year-in-mongodb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...