Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

MongoDB removing document

by GertMT (Friar)
on Oct 26, 2015 at 15:39 UTC ( #1145978=perlquestion: print w/replies, xml ) Need Help??

GertMT has asked for the wisdom of the Perl Monks concerning the following question:

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.

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 (Pope) on Oct 26, 2015 at 18:53 UTC

    Looking through the documentation, you seem to want to look at MongoDB::Collection instead of blindly calling methods on things that aren't objects.

      just to be clear.. I'm stuck. I did look at that page but haven't been able to find out how to get to a solution by implementing a 'filter expression'.

        I haven't done any work with MongoDB, but I assume from the following:

        # delete a document $coll->delete_one( { name => "John Doe" } );

        ... that you can delete a single document given its ID by using:

        $coll->delete_one( $doc ); # or maybe $coll->delete_one( { _id => $doc->{_id} } );
Re: MongoDB removing document
by NetWallah (Canon) on Oct 26, 2015 at 17:19 UTC
    The actual ID you are trying to delete has the VALUE 'something_unique' , and is of the TYPE/CLASS 'MongoDB::OID'.

    So - without knowing the MongoDB API, your error is passing in the TYPE, rather than the VALUE.

    I'd suggest:

    $doc->delete_one( {'_id'=>$doc->{_id} } ); # If that fails, try: $doc->delete_one( {'_id'=>$doc->{_id}{value} } );
    There are potentially other issues, but this should help step you past the current one.

    good luck.

            The best defense against logic is ignorance.

      Thank you for your reply

      Unfortunately I keep getting the message:

      Can't call method "delete_one" on unblessed reference at line...

      I'll investigate further.

        I know absolutely nothing about MongoDB, but I can tell you that you're trying to call a method on something that doesn't have any (a plain hash reference).

        In the MongoDB documentation's SYNOPSYS, it shows that to delete a document, you call the method against the collection ($coll) object like this: $coll->delete_one({ name => 'value' });

        For completeness, here's the expanded documentation specifically for the delete_one() method.

        When in doubt, RTFM ;)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1145978]
Approved by stevieb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2021-10-16 02:05 GMT
    Find Nodes?
      Voting Booth?
      My first memorable Perl project was:

      Results (69 votes). Check out past polls.