我有一个包含五十万个数据的集合,我希望输入日期之间的所有月份都为平均值。现在,我正在获取全年的数据,但我希望将其按单个月分开,即每个月12个数据范围。
下面是我正在使用的聚合管道。
let filter = 'y';
const date = new Date();
let checkDate = moment().subtract(1.5, 'years')._d;
MeterData.aggregate([
{
$group: {
_id: "$meter_id",
// total: { $sum: 1 },
totalEnergy: filter !== 'a' ? {
$sum: {
$toDouble: {
$cond: {
if: {
$gte: [
"$date", checkDate
]
},
then: "$energy.Energy",
else: 0
}
}
}
} : { $sum: { $toDouble:
"$energy.Energy"
} }
},
}
]);
在这里,我在totalEnergy字段中获得了全年的totalEnergy,但是现在我想要的是totalEnergy加上我输入年份的每月计算。 关于如何做到这一点的任何想法。 ?
下面是该集合中的示例文档。
{"_id":{"$oid":"5e557779ed588826d84cef11"},
"meter_id":"1001",
"date":{"$date":{"$numberLong":"1509474600000"}},
"parameter_name":"hvac","voltage":{"unit":"V"},
"current":{"unit":"AMP"},
"powerFactor":{"unit":"phi"},
"angle":{"unit":"degree"},
"activePower":{"unit":"kwh"},
"reactivePower":{"unit":"kwh"},
"apparentPower":{"unit":"kwh"},
"frequency":{"unit":"hz"},
"thd":{"unit":"percentage"},
"energy":{"Energy":"5.7"},
"power":{"unit":"watt"},
根据Ryan Gunner的建议,我得到了我要粘贴的答案,下面还有一个问题。
[
{
meter_id: '1001',
month: '2017-10',
totalEnergy: 0,
averageEnergy: 0
} + 11 more months......
]
现在我需要的是12个月的能量总量。例如,单个变量中所有12个月的totalEnergy字段的总计。
答案 0 :(得分:1)
这样的事情怎么样?
data class Cartdocs(
@SerializedName("id")
var id:String?=null,
@SerializedName("title")
var title:String?=null,
@SerializedName("stock")
var stock:Int?=null,
@SerializedName("mainImage")
var mainImage:String?=null,
@SerializedName("amount")
var amount:Int?=null,
@SerializedName("added")
var added:String?=null,
@SerializedName("options")
var options:ArrayList<cartOptions>,
var unitPoint:Int?=null,
@SerializedName("totalPrice")
var totalPrice:Int?=null,
@SerializedName("totalPoint")
var totalPoint:Int?=null
)
data class cartOptions(
var id:String?=null,
var description:String?=null
)
更新:添加了grandTotalEnergy字段,并将monthlyData推入数组。