#!/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 = ); # 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; }