我想在输出中投射'VBID'字段。但是,即使我在$ project中包含'VBID'字段,我也没有得到所需的输出。我错过了什么吗?
db.VehicleCount.aggregate([
{"$match": { "VBID" : "SL0008",
"Tstmp" : {"$gte" : 1506970800000}
}
},
{"$group": { "_id": {"$divide" : [{ "$subtract" : [{"$divide" : ["$Tstmp", 1000]}, { "$mod" : [{"$divide" : ["$Tstmp", 1000]}, 3600] }] }, 3600 ]},
"Car": {"$sum" : {"$cond": [ { "$eq": [ "$VType", 0 ] }, 1, 0 ]}},
"Large vehicle": {"$sum" : {"$cond": [ { "$eq": [ "$VType", 1 ] }, 1, 0 ]}},
"Bike": {"$sum" : {"$cond": [ { "$eq": [ "$VType", 2 ] }, 1, 0 ]}}
}
},
{"$project" : { "_id" : 1,
"Timestamp" : {"$multiply": ["$_id", 3600]},
"VBID" : 1,
"Car" : 1,
"Large vehicle" : 1,
"Bike" : 1
}
},
{"$sort" : { "Timestamp" : 1}}
])
由于
答案 0 :(得分:1)
您必须在 _id 字段中添加VBID。
db.VehicleCount.aggregate([
{"$match": { "VBID" : "SL0008",
"Tstmp" : {"$gte" : 1506970800000}
}
},
{"$group": {
"_id": {val:{"$divide" : [{ "$subtract" : [{"$divide" : ["$Tstmp", 1000]}, { "$mod" : [{"$divide" : ["$Tstmp", 1000]}, 3600] }] }, 3600 ]}
,VBID:'$VBID'},
"Car": {"$sum" : {"$cond": [ { "$eq": [ "$VType", 0 ] }, 1, 0 ]}},
"Large vehicle": {"$sum" : {"$cond": [ { "$eq": [ "$VType", 1 ] }, 1, 0 ]}},
"Bike": {"$sum" : {"$cond": [ { "$eq": [ "$VType", 2 ] }, 1, 0 ]}}
}
},
{"$project" : { "_id" : 1,
"Timestamp" : {"$multiply": ["$_id.val", 3600]},
"VBID" : "$_id.VBID",
"Car" : 1,
"Large vehicle" : 1,
"Bike" : 1
}
},
{"$sort" : { "Timestamp" : 1}}
])