Database

[{"_id":0,"time":"2020-06-01T09:48:50.886Z","tripid":1,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.4579241,50.88904930000001]},"vehid":1,"type":"car"},{"_id":1,"time":"2020-06-01T09:48:52.386Z","tripid":1,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.457893823793027,50.88902837053664]},"vehid":1,"type":"car"},{"_id":2,"time":"2020-06-01T09:48:57.523Z","tripid":1,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.457721,50.88890889999998]},"vehid":1,"type":"car"},{"_id":3,"time":"2020-06-01T09:37:19.215Z","tripid":11,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.306468999999999,50.873466799999974]},"vehid":2,"type":"truck"},{"_id":4,"time":"2020-06-01T09:37:20.715Z","tripid":11,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.306510889846841,50.87347702718032]},"vehid":2,"type":"truck"},{"_id":5,"time":"2020-06-01T09:37:21.465Z","tripid":11,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.306552779693681,50.873487254358416]},"vehid":2,"type":"truck"},{"_id":6,"time":"2020-06-01T09:05:57.834Z","tripid":19,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.3240439,50.87151239999999]},"vehid":3,"type":"truck"},{"_id":7,"time":"2020-06-01T09:05:59.334Z","tripid":19,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.324009224122276,50.871494384217556]},"vehid":3,"type":"truck"},{"_id":8,"time":"2020-06-01T09:06:01.134Z","tripid":19,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.323939872366828,50.87145835263179]},"vehid":3,"type":"truck"},{"_id":9,"time":"2020-06-01T09:06:01.421Z","tripid":19,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.323928799999999,50.87145259999999]},"vehid":3,"type":"truck"},{"_id":{"$oid":"65c2528fda6f6d73a2be4d52"},"time":"2020-06-01T09:06:26.621Z","tripid":19,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.323050772395544,50.87088438471755]},"vehid":3,"type":"truck"},{"_id":{"$oid":"65c267bdda6f6d73a2be4d5b"},"time":"2020-06-01T09:11:15.002Z","tripid":27,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.317372,50.80114849999999]},"vehid":4,"type":"car"},{"_id":{"$oid":"65c267bdda6f6d73a2be4d5c"},"time":{"$date":"2020-06-01T09:11:15.761Z"},"tripid":27,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.317350499999999,50.80114379999999]},"vehid":4,"type":"car"},{"_id":{"$oid":"65c267bdda6f6d73a2be4d5d"},"time":"2020-06-01T09:11:16.511Z","tripid":27,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.317365715252182,50.80111709098031]},"vehid":4,"type":"car"},{"_id":{"$oid":"65c267bdda6f6d73a2be4d5e"},"time":"2020-06-01T09:11:22.511Z","tripid":27,"geom_lnglat":{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[4.317517867774015,50.80084999994387]},"vehid":4,"type":"car"}]

Query

db.collection.aggregate([{$lookup:{from:"collection",as:"result",let:{cur_vehid:"$vehid",coordinates:"$geom_lnglat.coordinates",cur_point:"$geom_lnglat"},pipeline:[{$match:{$expr:{$lt:["$vehid","$$cur_vehid"]}}},{$set:{dist_from_point:{/** (lon_1 - lon_2)² + (lat_1 - lat_2)²*/$add:[{$pow:[{$subtract:[{$first:"$$coordinates"},{$first:"$geom_lnglat.coordinates"}]},2]},{$pow:[{$subtract:[{$last:"$$coordinates"},{$last:"$geom_lnglat.coordinates"}]},2]}]}}},{$group:{_id:{vehid:"$vehid"},distance:{$first:"$dist_from_point"}}}]}},{$unwind:{path:"$result"}},{$sort:{vehid:1,"result.distance":1}},{$group:{_id:{vehid:"$vehid",vehid2:"$result._id.vehid"},distance:{$first:"$result.distance"}}}])

Result