In MongoDB I have a structure like:
$VAR1 = {
'score' => '26.5249035452273',
'student_id' => 177,
'_id' => bless( {
'value' => 'something_unique'
}, 'MongoDB::OID' ),
'type' => 'homework'
};
BTW: yes this is homework
With a long list of documents (records) there are multiple documents per student_id and the first one appearing in the result-set needs to be removed.
I tried:
$doc->delete_one( {'_id'=>$doc->{'MongoDB::OID'}} ) and get Can't call method "delete_one" on unblessed reference
I don't know where to go from here.
Thanks for your help.
Gert
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 ) ] ); #multipleSor
+t
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'};
}
}