[英]Aggregate data from Mongo collection based on dates and other fields, and count
我會收集事件,按天計數。 如果事件發生一次,則為“命中”(billState),這是按位置和物料類別分類的。 我很難找回所需的數據,我嘗試了一些在SO上找到的示例,以及Mongo Docs中的許多示例。通常最后只得到了我需要刪除的部分內容。
我的收藏樣本如下:
{
"_id" : ObjectId("565ca8678e000995a09d1540"),
"company" : "someCompany",
"location" : "123",
"materialCode" : "MATCODE",
"materialClass" : "Class",
"totalCount" : 8,
"billState" : 1,
"eventTime" : ISODate("2015-11-30T19:49:59.243Z")
}
{
"_id" : ObjectId("565ca9778e000995a09d1541"),
"company" : "someCompany",
"location" : "1",
"materialCode" : "WTHFA",
"materialClass" : "OtherClass",
"totalCount" : 16,
"billState" : 1,
"eventTime" : ISODate("2015-11-30T19:54:31.695Z")
}
{
"_id" : ObjectId("565ca9778e000995a09d1541"),
"company" : "someCompany",
"location" : "12345",
"materialCode" : "WTHFA",
"materialClass" : "thirdClassOfMat",
"totalCount" : 16,
"billState" : 1,
"eventTime" : ISODate("2015-11-30T19:54:31.695Z")
}
我可以有幾個位置和materialClasses,我只想計算“ billState”是否為一個(容易,因為否則它將不在集合中)。 我需要按周,位置,日期,材料分類來細分它。
week1 mon tue wed thur fri sat sun
----------------------------
location 1 - - - Class otherClass = 2
location 123- - - Class otherClass = 2
week2 mon tue wed thur fre sat sun
----------------------------
locations material billState Count = X
-----
month total
目前,我只能(主要)基於另一篇SO帖子來了解這一點:
{
"_id" : 12,
"weeks" : {
"week" : 48,
"total" : 6,
"days" : [
{
"day" : ISODate("2015-12-02T00:00:00.000Z"),
"total" : 1
},
{
"day" : ISODate("2015-12-01T00:00:00.000Z"),
"total" : 1
},
{
"day" : ISODate("2015-11-30T00:00:00.000Z"),
"total" : 4
}
]
},
"monthTotal" : 6
}
這就是我現在所擁有的..
myCollection.aggregate([
// then total per day. Rounding dates
{ "$group": {
"_id": {
"$add": [
{ "$subtract": [
{ "$subtract": [ "$eventTime", new Date(0) ] },
{ "$mod": [
{ "$subtract": [ "$eventTime", new Date(0) ] },
1000 * 60 * 60 * 24
]}
]},
new Date(0)
]
},
"week": { "$first": { "$week": "$eventTime" } },
"month": { "$first": { "$month": "$eventTime" } },
"total": { "$sum": "$billState" }
}},
// Then group by week
{ "$group": {
"_id": "$week",
"month": { "$first": "$month" },
"days": {
"$push": {
"day": "$_id",
"total": "$total"
}
},
"total": { "$sum": "$total" }
}},
// Then group by month
{ "$group": {
"_id": "$month",
"weeks": {
"$push": {
"week": "$_id",
"total": "$total",
"days": "$days"
}
},
"monthTotal": { "$sum": "$total" }
}},
{"$unwind": "$weeks"},
{ $out : "billingTotals" }
]);
};
我嘗試使用更多的$ groups,$ match和$ project搞亂了,但是似乎無法按日期和位置將其分解得足夠遠。 真的,我只需要統計每天,地點和材料類別的事件,然后每周和每月總計。 因此,在任何給定的一天,一個位置可能有20種材料類別的匹配,另一位置可能有“ X”個數量的匹配,依此類推。我每天只計算一次每個位置,每個位置的匹配。
編輯:輸出示例(我認為這是一個好主意..,這已經是漫長的一天了)
{
"month" : 12 {
"week" : 49 {
"day" : 3 {
"location": "123",
"materials": [
{
"class": "materialClass",
"total" : 2
},
{
"class": "otherMatClass",
"total" : 5
}
],
"location": "1234",
"materials": [
{
"class": "materialClass",
"total" : 2
},
{
"class": "otherMatClass",
"total" : 5
}
],
},
"day" : 4 {
"location": "123",
"materials": [
{
"class": "materialClass",
"total" : 2
},
{
"class": "otherMatClass",
"total" : 5
}
]
}
},
"week" : 50 {
"day" : 3 {
"location": "123",
"materials": [
{
"class": "materialClass",
"total" : 2
},
{
"class": "otherMatClass",
"total" : 5
}
]
}
}
}
}
自從我在MongoDB中使用聚合方法以來已經有一段時間了。 我將其與您的模擬數據放在一起。 不幸的是沒有得到確切的結果。 也許它仍然會有所幫助。 祝好運。
db.materials.aggregate([
{
$match: {billState:{$ne:0}}
},
{
$group: {
_id:{location:"$location",date:{week:{$week:"$eventTime"},month:{$month:"$eventTime"},day:{$dayOfMonth:"$eventTime"},year:{$year:"$eventTime"}}},
materials:{$addToSet:{code:"$materialCode",class:"$materialClass"}},
}
},
{
$sort:{"_id.date": 1}
},
{
$group: {
_id:{date:"$_id.date"},
locations: {$addToSet: {location:"$_id.location",materials:"$materials"}},
}
},
{
$project:{_id:0, date:"$_id.date",locations:1}
}
]).pretty()
查詢返回結果
{
"locations" : [
{
"location" : "123",
"materials" : [
{
"code" : "MATCODE",
"class" : "Class"
}
]
},
{
"location" : "1",
"materials" : [
{
"code" : "WTHFA",
"class" : "OtherClass"
}
]
},
{
"location" : "12345",
"materials" : [
{
"code" : "WTHFA",
"class" : "thirdClassOfMat"
},
{
"code" : "WTHFC",
"class" : "thirdClassOfMatter"
}
]
}
],
"date" : {
"week" : 48,
"month" : 11,
"day" : 30,
"year" : 2015
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.