Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Creating a perl delete statement with MongoDB

by e0s (Novice)
on Jul 20, 2017 at 12:44 UTC ( [id://1195603]=perlquestion: print w/replies, xml ) Need Help??

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

I am having difficulty creating a perl MongoDB delete statement that will select from EITHER one table or another, depending on the TYPE of input from the user. Let's say the databases are $FRUITS and each fruit is categorized into tables either by their 'fruit_names' or unique 'fruit_ids'

Let's say the user wants to delete an entry from the fruit_names table, how do I configure my perl code so that it can differentiate between one input versus another?

I've searched cpan.org and found some MongoDB documentation on delete_one and delete_many but I haven't been able to figure out how I would create a delete_one statement that would pull from either one table or the other, depending on the user's input.

I am a novice with perl still and I've never used MongoDB before, i've been really stuck on this for a while so any help would be greatly, greatly appreciated.

  • Comment on Creating a perl delete statement with MongoDB

Replies are listed 'Best First'.
Re: Creating a perl delete statement with MongoDB
by poj (Abbot) on Jul 20, 2017 at 16:19 UTC

    Not sure why you want to separate a fruit NAME from it's ID by using 2 collections. Maybe this demo will help you explain.

    #!/usr/bin/perl use strict; use MongoDB; # connect to database my $client = MongoDB->connect( 'mongodb://localhost:27017' ); my $db = $client->get_database( 'test' ); $db->drop; # start with empty database # insert some test data my @fruits = qw(apple banana cherry damson elderberry fig grape kiwi mango orange pear raspberry strawberry tangerine ugli watermelon); my $fruits = $db->get_collection('fruits'); my $id = 1; for (@fruits){ # put ID and NAME in same collection $fruits->insert_one({ uid => "$id", name => $_}); ++$id; } # show table and get user input my $entry; while ($entry ne 'q') { my $all_fruits = $fruits->find; while (my $doc = $all_fruits->next) { printf " %2s %s\n",$doc->{'uid'},$doc->{'name'}; } print "Enter ID or Name (q to quit) > "; chomp($entry = <STDIN>); # use OR for either ID or Name my $res = $fruits->delete_one( { '$or' => [ {uid=>$entry}, {name=>$entry} ] } ); printf "\n[$entry] %d records deleted \n",$res->deleted_count; }
    poj
      thank you so much, that's actually really helpful.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Creating a perl delete statement with MongoDB
by Theodore (Friar) on Jul 20, 2017 at 13:02 UTC
    How would you differentiate between one input versus another?
    For example, if you expect all names to be strings and all ids to be integers, you could use looks_like_number from Scalar::Util to decide if the input is a name or an id and delete from the corresponding table collection.
Re: Creating a perl delete statement with MongoDB
by thanos1983 (Parson) on Jul 20, 2017 at 13:03 UTC
      definitely forgot to post my code. sorry.

      at this point i've tried creating a subroutine that is attempting to do this:

      sub delete_thefruits my $collection; my $criteria;
      for (ref $entry){ ## i think this will check for the type being input +by the user when ('fruitdb::name') { $collection = $FRUIT_NAMES; $criteria->(name) = $entry->{data}{name};
      If i create a similar function to the above, another one which only deals with the fruitIDs table, would perl understand to differentiate between the two?? or would I need to illustrate that programmatically?
      $collection-> remove(criteria); ## this will hopefully eventually remo +ve one entry whether it's fruit name or fruitID }
        for (ref $entry){ ## i think this will check for the type being input +by the use
        No, it will not.

        Hello again eOs,

        What I would do in your case is try to create a minimal working example for everyone here to play. For example, create db, insert data, then retrieve data, delete and retrieve again.

        It is really difficult to guess what is returned and what it should be deleted (at least to me). You have the code in you node and you can execute it at any time but we do not have it, so guessing is not the best way to proceed.

        Help us to help you, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-24 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found