我试图在24小时内获取三个不同时间范围的第一个和最后一个文档,然后按天分组。
一天中的时间范围
第1天
T1:06:00-17:00-获取第一个和最后一个文件
T2:17:00-22:00-获取第一个和最后一个文件
T3:22:00-06:00-获取第一个和最后一个文件
第二天..等等
示例文档
{
"_id" : ObjectId("5ba6bc27260d0909e43b0874"),
"_DeviceName" : "Ground Floor",
"PointType" : "Building",
"_DeviceID" : ObjectId("5b9ae3a76080700be0f173d6"),
"_TariffID" : ObjectId("5a8893216b41bd32c0797f91"),
"_Timestamp" : ISODate("2018-09-22T22:03:18.552+0000"),
"_ModbusID" : 1,
"_LocationID" : ObjectId("5b9ae0eb6080700be0f173bf"),
"Registers" : {
"quadrant4reactiveenergyL3" : 781.0,
"quadrant4reactiveenergyL2" : 74.0,
"quadrant4reactiveenergyL1" : 144.0,
"quadrant1reactiveenergyL3" : 52.0,
"quadrant1reactiveenergyL2" : 706.0,
"quadrant1reactiveenergyL1" : 185.0,
"totalharmonicdistorsionVL3" : 3.3000000000000003,
"totalharmonicdistorsionVL2" : 3.5,
"totalharmonicdistorsionVL1" : 3.6,
"consumedactiveenergyL3" : 9144.0,
"consumedactiveenergyL2" : 21774.0,
"consumedactiveenergyL1" : 18509.0,
"totalconsumedactiveenergy" : 49445.0,
}}
我试图做的是以下几点; 按时间分组,但无法获取每个条件的第一个和最后一个元素。
"$project": {
"yearMonthDayUTC": {
"$dateToString": {
"format": "%Y-%m-%d",
"date": "$_Timestamp"
}
},
"timewithOffset": {
"$dateToString": {
"format": "%H:%M:%S",
"date": "$_Timestamp"
}
},
"Registers.totalconsumedactiveenergy": 1.0,
"_DeviceID": 1.0
}
"$group": {
"_id": {
"Date": "$yearMonthDayUTC",
"Device": "$_DeviceID"
},
"T1": {
"$sum": {
"$cond": [
{
"$and": [
{
"$gte": [
"$timewithOffset",
"06:00:00"
]
},
{
"$lte": [
"$timewithOffset",
"17:00:00"
]
}
]
},
1.0,
0.0
]
}
},
"T2": {
"$sum": {
"$cond": [
{
"$and": [
{
"$gte": [
"$timewithOffset",
"17:00:00"
]
},
{
"$lte": [
"$timewithOffset",
"22:00:00"
]
}
]
},
1.0,
0.0
]
}
},
"firstactive": {
"$first": "$$ROOT.Registers.totalconsumedactiveenergy"
},
"lastactive": {
"$last": "$$ROOT.Registers.totalconsumedactiveenergy"
}
}
},
我希望看到这样的东西
第1天:2018年12月12日
T1-第一个文档,最后一个文档
T2-第一个文档,最后一个文档
T3-第一个文档,最后一个文档
第2天:2018年12月13日
T1-第一个文档,最后一个文档
T2-第一个文档,最后一个文档
T3-第一个文档,最后一个文档
...
答案 0 :(得分:1)
请尝试以下聚合管道,如果您需要完整的done < /home/afsara/Desktop/ns2_offline/ns_code/wired.out;
和 #!/bin/bash
cd /
cd /home/afsara/Desktop/ns2_offline/ns_code/
#INPUT: output file AND number of iterations
output_file_format="tcp";
iteration_float=2.0;
end=5
iteration=$(printf %.0f $iteration_float);
r=5
while [ $r -le $end ]
do
###############################START A ROUND
l=1;thr=0.0;val=0.0
i=0
while [ $i -lt $iteration ]
do
while read val
do
dir="/home/afsara/Desktop/ns2_offline/ns_code/"
#dir=""
under="_"
all="all"
output_file="$dir$output_file_format$under$r$under$r$under$all.out"
echo -ne "Throughput: $thr " > $output_file
if [ $l == '1' ]; then
thr=$(echo "scale=5; $thr+$val/$iteration_float" | bc )
echo -ne "throughput: $val " >> $output_file
fi
echo "$val"
done < /home/afsara/Desktop/ns2_offline/ns_code/wired.out; #problem because of this
i=$(($i+1))
l=0
#################END AN ITERATION
done
r=$(($r+1))
#######################################END A ROUND
done
文档,则需要使用$first
$last