Integrated code updateCollectionStats code (previously a Stored Procedure)