- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好,我的问题是如何从数据库获取最近四个月的记录。我得到了输出,但没有得到我预期的模式,所以如何做到这一点任何人都可以帮助我解决这个问题。
db.Air_pollution.aggregate([
{$match:
{CREATE_DATE:{$lte:new Date(),
$gte:new Date(new Date().setDate(new
Date().getDate()-120))}}},
{
$group:
{
_id:{month:{$month:"$CREATE_DATE"},
year:{$year:"$CREATE_DATE"}
},
avgofozone:{$avg:"$OZONE"}
}
},
{
$sort:{"year":-1}
},
{
$project:{
year:'$_id.year',
avgofozone:'$avgofozone',
month:'$_id.month',_id:0
}
}
])
输出是:
{ "avgofozone" : 21.07777777777778, "year" : 2018, "month" : 2 }
{ "avgofozone" : 17.8, "year" : 2018, "month" : 3 }
{ "avgofozone" : 17.8, "year" : 2018, "month" : 1 }
预期输出:
zone_type year january febravary march
avgofozone 2018 17.8 21.07777 17.8
最佳答案
您已经拥有所需的数据,而不是尝试从 MongoDB 中准确获取所需的数据,为什么不在应用程序层中对其进行格式化呢?
一些简单的 JavaScript 可以做到这一点:
const months = [
'January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September',
'October', 'November', 'December'
];
function monthNumToName(monthnum) {
return months[monthnum - 1];
}
const results = [{
"avgofozone": 21.07777777777778,
"year": 2018,
"month": 2
}, {
"avgofozone": 17.8,
"year": 2018,
"month": 3
}, {
"avgofozone": 17.8,
"year": 2018,
"month": 1
}];
const statsByYear = results.reduce((a, c) => {
const {
year,
month,
avgofozone
} = c;
if (!a[year])
a[year] = {
zone_type: 'avgofozone',
year,
[monthNumToName(month)]: avgofozone
}
else a[year][monthNumToName(month)] = avgofozone
return a;
}, {});
const formatted = Object.keys(statsByYear).map(k => {
return statsByYear[k];
});
console.log(formatted);
关于javascript - 如何使用芒果聚合获得另一种模式的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49443346/
是否可以用Java开发wp7.5(mango)应用程序?我在 wp7 启动期间所知道的是,它暂时没有任何适用于 wp7 的 java 运行时。现在怎么样? 谢谢。 最佳答案 没有。目前开发 Windo
我正在使用以下代码在设备上创建动态磁贴: ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault(); StandardTileData newTi
是否可以使用 Cloudant Query/CouchDB 2.0 Find 设置 $regex 标志? 具体来说,我想要一个不区分大小写的搜索,全局也很有用。 在 JavaScript 中我会这样做
我正在向我的页面动态添加内容(使用 AJAX)。这工作正常,除了不会自动应用的样式。解决此问题的方法是使用 .trigger("create");(例如 $('#content').html(data
我正在尝试在删除父行时删除 Db 中的所有子级的两列之间建立 FK 关系。我的定义如下: [Table] public class Parent { [Colu
我有以下问题: 我将 facebook-c#-sdk 用于 Windows Phone 7.1(芒果版),取自此处: http://facebooksdk.codeplex.com/releases/
我是一名优秀的程序员,十分优秀!