如何计算mongodb中的YTD和MTD?

时间:2016-11-14 01:52:49

标签: mongodb

如何在单个查询中计算mongodb中的月初至今(MTD)和年初至今(YTD)?下面的样本数据,在这个数据中,requestOn是一个日期字段,我想计算MTD& YTD,假设财政年度为"今年1月1日"(例如2016年的财政年度是" 2016年1月1日和#34;:

     {
  "_id": {
    "$oid": "5808578b33fa6f161c9747f8"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2000-03-01",
  "bookName": "Test6",
  "revenue": 10.0,
  "unitsSold": 1,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "5808578b33fa6f161c9747f9"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2000-03-01",
  "bookName": "Test1",
  "revenue": 11.0,
  "unitsSold": 2,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "5808578b33fa6f161c9747fa"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2000-06-01",
  "bookName": "Test2",
  "revenue": 12.0,
  "unitsSold": 3,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "5808578b33fa6f161c9747fb"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2000-07-01",
  "bookName": "Test3",
  "revenue": 13.0,
  "unitsSold": 4,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "5808578b33fa6f161c9747fc"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2009-09-01",
  "bookName": "Test4",
  "revenue": 14.0,
  "unitsSold": 5,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "5808578b33fa6f161c9747fd"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2009-06-01",
  "bookName": "Test5",
  "revenue": 15.0,
  "unitsSold": 6,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "5808578b33fa6f161c9747fe"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2004-06-01",
  "bookName": "Test10",
  "revenue": 16.0,
  "unitsSold": 7,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "5808578b33fa6f161c9747ff"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2000-01-01",
  "bookName": "Test11",
  "revenue": 100.0,
  "unitsSold": 100,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    },
    {
      "categoryCode": "Cooking/Beverages/Bartending"
    },
    {
      "categoryCode": "Food Receipe/Taste"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e462"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2000-02-01",
  "bookName": "Test1",
  "revenue": 20.0,
  "unitsSold": 10,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e463"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2001-02-01",
  "bookName": "Test2",
  "revenue": 19.0,
  "unitsSold": 9,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e464"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2001-02-01",
  "bookName": "Test3",
  "revenue": 18.0,
  "unitsSold": 8,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e465"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2007-06-01",
  "bookName": "Test4",
  "revenue": 17.0,
  "unitsSold": 7,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e466"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2005-06-01",
  "bookName": "Test5",
  "revenue": 16.0,
  "unitsSold": 6,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e467"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2004-06-01",
  "bookName": "Test1",
  "revenue": 15.0,
  "unitsSold": 5,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e468"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2002-06-01",
  "bookName": "Test2",
  "revenue": 14.0,
  "unitsSold": 4,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e469"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2001-06-01",
  "bookName": "Test3",
  "revenue": 13.0,
  "unitsSold": 3,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e46a"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2000-06-01",
  "bookName": "Test4",
  "revenue": 12.0,
  "unitsSold": 2,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}{
  "_id": {
    "$oid": "580857b833fa6f0c3499e46b"
  },
  "_class": "exceltest.TestBean",
  "requestedOn": "2008-06-01",
  "bookName": "Test5",
  "revenue": 11.0,
  "unitsSold": 1,
  "bookCategory": [
    {
      "categoryCode": "Cooking/"
    },
    {
      "categoryCode": "Cooking/Beverages"
    },
    {
      "categoryCode": "Food Receipe/"
    },
    {
      "categoryCode": "Food Receipe/Bartending"
    }
  ]
}

此致

克里斯

1 个答案:

答案 0 :(得分:0)

将MongoDB中的日期保留在其原生日期格式ISODate()中是一种很好的做法。

您可以使用日期格式like $ year,$ month,$ day,$ hour等。

在您的情况下,这些可用于分组:

GroupIds

将字符串转换为ISODate,答案可以在

找到
    db.collectionName.aggregate([
          {$group:{_id:{'Date':{$year:'$requestedOn'}},total:{$sum:'$FieldName'}}}
     ])