Fixed database aggregation bug