GertMT has asked for the wisdom of the Perl Monks concerning the following question:
In MongoDB I have a structure like:
BTW: yes this is homework$VAR1 = { 'score' => '26.5249035452273', 'student_id' => 177, '_id' => bless( { 'value' => 'something_unique' }, 'MongoDB::OID' ), 'type' => '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.
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'}; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: MongoDB removing document
by Corion (Patriarch) on Oct 26, 2015 at 18:53 UTC | |
by GertMT (Hermit) on Oct 26, 2015 at 19:09 UTC | |
by Corion (Patriarch) on Oct 26, 2015 at 19:14 UTC | |
by GertMT (Hermit) on Oct 26, 2015 at 19:24 UTC | |
Re: MongoDB removing document
by NetWallah (Canon) on Oct 26, 2015 at 17:19 UTC | |
by GertMT (Hermit) on Oct 26, 2015 at 18:07 UTC | |
by stevieb (Canon) on Oct 26, 2015 at 18:29 UTC | |
by GertMT (Hermit) on Oct 26, 2015 at 18:48 UTC |
Back to
Seekers of Perl Wisdom