gpt4 book ai didi

java - 在 MongoDB Java 驱动程序中聚合时解析数据类型

转载 作者:行者123 更新时间:2023-12-01 18:41:51 27 4
gpt4 key购买 nike

我创建了一个每小时的时间段,因为我想对我的数据进行分组并每小时进行一些计算(Sum、Avg、Stdv、Max、Min)。这是我的代码:

Bson startTimeBucket = match( gt("lastTimestamp",startTime));
Bson endTimeBucket= match(lte("lastTimestamp",finishTime));
Bson idFilterBucket = match(eq("id", IdManfc));
Bson unwindBucket = unwind("$signals");
Bson filterSignalUnBucket = match(eq("signals.signal",
mysignal));

Bson Bucket=bucket("$lastTimestamp", timeBucketList, new BucketOptions()
.defaultBucket("sum")
.output( min("id","$id"),
min("signal","$signals.signal"),
sum("Summation","$signals.value"),
avg("Average","$signals.value"),
stdDevSamp("STD","$signals.value"),
max("Max","$signals.value"),
min("Min","$signals.value")
)
);

List<Document> resultbuckt = coll.aggregate(asList(startTimeBucket,
endTimeBucket,
idFilterBucket,
unwindBucket,
filterSignalUnBucket,
Bucket
)).into(new ArrayList<Document>());
for (Document Document : resultbuckt) {
System.out.println(Document);
}

我的问题是我看到了结果,但总和、平均值和标准差为零或为空。我只看到最小值和最大值。我猜测是因为数据类型是字符串类型,它不能应用数学运算,而对于最小值和最大值,它甚至可以作为字符串应用。有谁知道如何在聚合时将数据类型解析为双倍?

我的结果的一行:

Document{{_id=1574431200859, id=144-12, signal=InterfaceType.InjectionUnits.InjectionUnit_1.TemperatureZones.TemperatureZone_1.ActualTemperature, Summation=0, Average=null, STD=null, Max=72.0, Min=68.0}}

如果有人有任何提示,我将不胜感激。

谢谢

最佳答案

找到解决方案!

        Bson startTimeBucket = match( gt("lastTimestamp",startTime));
Bson endTimeBucket= match(lte("lastTimestamp",finishTime));
Bson idFilterBucket = match(eq("id", IdManfc));
Bson unwindBucket = unwind("$signals");
Bson filterSignalUnBucket = match(eq("signals.signal", mysignal));

Bson Bucket=bucket("$lastTimestamp", timeBucketList, new BucketOptions()
.defaultBucket("sum")
.output(min("id","$id"),
min("signal","$signals.signal"),
avg("Average",Document.parse("{$toDouble: \"$signals.value\"}")),
stdDevSamp("STD",Document.parse("{$toDouble: \"$signals.value\"}")),
max("Max",Document.parse("{$toDouble: \"$signals.value\"}")),
min("Min",Document.parse("{$toDouble: \"$signals.value\"}"))
)
)

和一行结果:

Document{{_id=1574431200859, id=144-12, signal=InterfaceType.InjectionUnits.InjectionUnit_1.TemperatureZones.TemperatureZone_1.ActualTemperature, Average=69.98214285714286, STD=1.243032529795528, Max=72.0, Min=68.0}}

关于java - 在 MongoDB Java 驱动程序中聚合时解析数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917799/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com