{
"_id" : ObjectId("528dd86bd6188ad3e9c0c8ea"),
"id" : "CA002303986",
"data" : NumberLong(20000101),
"element" : "TMAX",
"value1" : -300,
"mflag1" : "",
"qflag1" : "",
"sfalg1" : "G",
"lcol" : ""
}
{
"_id" : ObjectId("528dd86bd6188ad3e9c0c8eb"),
"id" : "CA002303986",
"data" : NumberLong(20000101),
"element" : "TMIN",
"value1" : -345,
"mflag1" : "",
"qflag1" : "",
"sfalg1" : "G",
"lcol" : ""
}
作为NumberLong的字段数据实际上是日期。我想在它的前4位数字上使用group by function。在MongoDB中有没有办法做到这一点? 我能够在MySQL中将它首先转换为字符串,然后在子字符串上运行子字符串并运行group by。但是MySQL有很多数据需要处理。
任何帮助将不胜感激。
答案 0 :(得分:2)
db.collection.aggregate({$project:{year:{$substr:["$data",0,4]}}},
{$group:{_id:{'y':'$year'},count:{$sum:1}}})