我们的部分数据是这样的......
{
"imei": "LDSJB0005",
"timestamp": "2017-11-11 14:49:26",
"trip_id" : 16,
"data_type": 2,
"longitude": "-",
"latitude": "-",
"loc_wgs84": {
"type": "Point",
"coordinates": [
0,
0
]
}
}
db.vehicles中有300万条这样的条目,索引是这样的。
> db.vehicles.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "demo.vehicles"
},
{
"v" : 2,
"key" : {
"imei" : 1,
"data_type" : 1,
"trip_id" : 1
},
"name" : "imei_1_data_type_1_trip_id_1",
"ns" : "demo.vehicles"
},
{
"v" : 2,
"key" : {
"loc_wgs84" : "2dsphere"
},
"name" : "loc_wgs84_2dsphere",
"ns" : "demo.vehicles",
"2dsphereIndexVersion" : 3
}
]
现在,我们正在进行如下聚合查询。试图让每辆车的imei具有最大的trip_id值(这意味着它是当天的最后一辆车)
[
{$match: {'data_type': 1}},
{$sort: {imei:1, trip_id:-1}},
{$group: {
_id: '$imei',
imei: {$first: '$imei'},
trip_id: {$first: '$trip_id'}
}}
]
查询需要10分钟才能完成。帮助