Implement mongoDB aggregate function.