Added mongoDB query to distance function