$VAR1 = { 'score' => '26.5249035452273', 'student_id' => 177, '_id' => bless( { 'value' => 'something_unique' }, 'MongoDB::OID' ), 'type' => 'homework' }; #### use strict; use warnings; use MongoDB; my $client = MongoDB->connect(); my $db = $client->get_database('students'); my $coll = $db->get_collection('grades'); my $result = $coll->find( { 'type' => 'homework' } ) ->sort( [ ( 'student_id' => 1 ), ( 'score' => 1 ) ] ); #multipleSort my $previous = "1"; while ( my $doc = $result->next ) { # print $doc->{'student_id'} . " " . $doc->{'score'} . "\n"; # debug if ( $doc->{'student_id'} != $previous ) { # the following doesn't work.... # $doc->delete_one( {'_id'=>$doc->{'MongoDB::OID'}} ); $previous = $doc->{'student_id'}; } }