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