MongoDB Java驱动程序中的存储桶聚合和分组

我做了简短的查询,以使用MongoDB Java Driver v 3.2从MongoDB中获取一些数据。我正在使用 Bucket and Unwind聚合使我的工作和结果变得有意义。我根据特定的id字段( IdManfc )和特定的信号字段( mysignal )进行了过滤。我的目标是使用存储桶聚合来基于某些边界(每小时时间戳)将数据分组。我想在结果中显示 id 信号,这就是为什么我使用传统的SQL技巧( min(id),因为我仅过滤了一个ID)和(最小(信号),因为我仅过滤了一个信号),以便在结果中包含我的 id 信号。现在,我想改变一点理论,我想像一下,我只有一个一个ID ,但是却有一个许多信号,并且我想将它们包括在结果中id是一个,但每个信号的信息都不同。或者,如果我有很多Id和很多信号,并且想要将它们包括在结果中怎么办。有人知道吗?

关于我的代码中 id 信号的含义的一些提示,id表示工厂中的机器,信号表示该机器执行的工作类型。假设工厂的一台计算机的ID为12345,它执行3种类型的工作,这些工作由信号标识,一个信号是打印(信号名称X),第二个信号是正在扫描(Y),第三个信号是不起作用(Z)。现在,我想制作一个桶,指定机器ID(12345)及其工作类型,每小时进行分组,并计算每个信号所用时间的最小值和最大值(这是值)。

    Bson startTimeBucket = match( gte("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",max("Max",min("Min",Document.parse("{$toDouble: \"$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);

我在数据库中的数据: enter image description here

对应的JSON:

{"_id":{"$oid":"5db06b6880bd68ca32a853bc"},"disconnected":false,"id":"144-12","lastTimestamp":{"$numberLong":"1571842909626"},"manufacturer":"gmb","modelName":"GMB_Mod1","signals":[{"event":"changed","signal":"InterfaceType.Moulds.Mould_1.TemperatureZones.TemperatureZone_4.actualTemperature","stability":"unstable","timestamp":{"$numberLong":"1571842907576"},"type":"DOUBLE","value":"175.0","writable":"read","written":false},{"event":"changed","signal":"InterfaceType.InjectionUnits.InjectionUnit_1.TemperatureZones.TemperatureZone_3.actualTemperature","value":"35.0","written":false}],"tC":{"$numberInt":"10"}}
{"_id":{"$oid":"5db069c480bd68ca32a841ec"},"lastTimestamp":{"$numberLong":"1571842499744"},"signal":"InterfaceType.InjectionUnits.InjectionUnit_1.TemperatureZones.TemperatureZone_1.actualTemperature","timestamp":{"$numberLong":"1571842496543"},"value":"65.0","tC":{"$numberInt":"10"}}

我的结果的2行是相同的信号和不同的时间(因为我使用时段和时间边界将结果分组:

Document{{_id=1574434800859,id=144-12,signal=signal 1,Average=69.86274509803921,STD=1.2003267528991806,Max=71.0,Min=68.0}}
Document{{_id=1578444800859,Max=72.0,Min=69.0}}

在我的代码中,我专门过滤了一个信号,然后在我的结果中看到它,我使用了min(signal)。 我希望从结果中看到的是与我已经得到的类似的东西。但是我还希望在该时间边界(“ _id)中获得其他信号的结果,并删除仅一个信号的过滤器,我希望如下所示:

Document{{_id=1574434800859,signal=**the name of 1st signal**,STD=1.20035267528991806,Max=80.0,Min=68.0}}
Document{{_id=1574434800859,signal=**the name 2nd signal**,STD=12.2003267528991806,Max=95.0,signal=**the name of 3rd signal**,STD=0.2003267528991806,Max=85.0,Min=68.0}}

有人知道吗?

谢谢

anita999 回答:MongoDB Java驱动程序中的存储桶聚合和分组

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2677538.html

大家都在问