6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MongoDB on Perl

Posted at

MongoDBをPerlで使うにあたり、一番最初に動作確認した時のスクリプトを整理しました。とりあえず最低限の動作確認はこれで十分かな。後は実際に使う時にドキュメントを調べればいいと思います。

####ネストされたHashにすら正規表現が使えるところが素敵

でも、ネストされた hash 内を正規表現を使って検索する例が意外とWeb上に例がなく、試行錯誤を繰り返してしまいました。Query 5 がそれです。出来てしまえば単純なことだったんですけどね。Query 4 にあるように、まずネストされた hash の記述の仕方を確認するべきでした。急いてしまって、いきなりネストの情報にアクセスしようとして一時は「できないのか…」と諦めかけてしまいました。

下のスクリプトは最後に collectiondb を削除しているので注意してください。

use strict;
use utf8;
use open ":utf8";
use open ":std";
use MongoDB;
use MongoDB::OID;

# http://search.cpan.org/~friedo/MongoDB/

# Make a client object.
my $client = MongoDB::MongoClient->new;

# my $client = MongoDB::MongoClient->new("host"     => "localhost:27017",
#                                        "username" => "foo",
#                                        "password" => "bar");

# Make database object.
my $db = $client->get_database( 'test' );

# Make a collection(table) object.
my $atomics = $db->get_collection( 'atomic_symbol' );

# Insert a document(record) object.
$atomics->insert( 
                    {"Z"      => 1,
                     "symbol" => 'H',
                     "name"   => {'ja' => '水素', 'en' => 'Hydrogen'},
                     "period" => 1
                    }
                );

# Insert some documents using array.
my @data = ();
push @data, { 'Z'=>2,   'symbol'=>'He', 'period'=>1, 'name'=>{'en'=>'Helium',      'ja'=>'ヘリウム'} };
push @data, { 'Z'=>3,   'symbol'=>'Li', 'period'=>2, 'name'=>{'en'=>'Lithium',     'ja'=>'リチウム'} };
push @data, { 'Z'=>13,  'symbol'=>'Al', 'period'=>3, 'name'=>{'en'=>'Aluminum',    'ja'=>'アルミニウム'} };
push @data, { 'Z'=>30,  'symbol'=>'Zn', 'period'=>4, 'name'=>{'en'=>'Zinc',        'ja'=>'亜鉛'} };
push @data, { 'Z'=>36,  'symbol'=>'Kr', 'period'=>4, 'name'=>{'en'=>'Krypton',     'ja'=>'クリプトン'} };
push @data, { 'Z'=>96,  'symbol'=>'Cm', 'period'=>7, 'name'=>{'en'=>'Curium',      'ja'=>'キュリウム'} };
push @data, { 'Z'=>116, 'symbol'=>'Lv', 'period'=>7, 'name'=>{'en'=>'Livermorium', 'ja'=>'リバモリウム'} };
# Insert
my @ids = $atomics->batch_insert( \@data );

# Check id numbers.
foreach my $idn ( @ids ){
    print "_id : $idn\n";
}

# Query 1
print "\nQuery 1\n";
my $some_atomics = $atomics->find( {'symbol' => 'He'} );
while ( my $atom = $some_atomics->next ){
    print "Symbol : $atom->{'symbol'}\n";
}

# Query 2
print "\nQuery 2\n";
$some_atomics = $atomics->find( {'symbol'=>qr/.+N/i} );
while ( my $atom = $some_atomics->next ){
    print "Symbol : $atom->{'symbol'}\n";
}

# Query 3
print "\nQuery 3\n";
$some_atomics = $atomics->find({ 'Z'=> {'$lte'=>30} });
while ( my $atom = $some_atomics->next ){
    print "Z : $atom->{'Z'} / Symbol : $atom->{'symbol'}\n";
}

# Query 4
print "\nQuery 4: Hash in Hash\n";
my $all_atomics = $atomics->find( {'name.en'=>'Zinc'} );
while ( my $atom = $all_atomics->next ){
    print "Symbol : $atom->{'symbol'} / Name : $atom->{'name'}->{'ja'}\n";
}

# Query 5: Hash in Hash with Regex
print "\nQuery 5: Hash in Hash with Regex\n";
$all_atomics = $atomics->find( { 'name.en'=> qr/.+iu.+/ } );
while ( my $atom = $all_atomics->next ){
    print "Symbol : $atom->{'symbol'} / Name : $atom->{'name'}->{'ja'} / $atom->{'name'}->{'en'}\n";
}

# Update
# update (\%criteria, \%object, \%options?)
print "\nUpdate\n";
$atomics->update( { 'Z'=>{'$lte'=>30} },
                  { '$inc'=>{'Z'=>1} },
                  { 'upsert' => 1, 'multiple' => 1} );
# Check
$all_atomics = $atomics->find;
while ( my $atom = $all_atomics->next ){
    print "Z : $atom->{'Z'} / Symbol : $atom->{'symbol'} / Name : $atom->{'name'}->{'ja'}\n";
}

# Delete Objects
$atomics->drop;
$db->drop;

##Result

$ perl mongoDB.pl
_id : 53f307c733519809206e5ed2
_id : 53f307c733519809206e5ed3
_id : 53f307c733519809206e5ed4
_id : 53f307c733519809206e5ed5
_id : 53f307c733519809206e5ed6
_id : 53f307c733519809206e5ed7
_id : 53f307c733519809206e5ed8

Query 1
Symbol : He

Query 2
Symbol : Zn

Query 3
Z : 1 / Symbol : H
Z : 2 / Symbol : He
Z : 3 / Symbol : Li
Z : 13 / Symbol : Al
Z : 30 / Symbol : Zn

Query 4: Hash in Hash
Symbol : Zn / Name : 亜鉛

Query 5: Hash in Hash with Regex
Symbol : He / Name : ヘリウム / Helium
Symbol : Li / Name : リチウム / Lithium
Symbol : Cm / Name : キュリウム / Curium
Symbol : Lv / Name : リバモリウム / Livermorium

Update
Z : 2 / Symbol : H / Name : 水素
Z : 3 / Symbol : He / Name : ヘリウム
Z : 4 / Symbol : Li / Name : リチウム
Z : 14 / Symbol : Al / Name : アルミニウム
Z : 31 / Symbol : Zn / Name : 亜鉛
Z : 36 / Symbol : Kr / Name : クリプトン
Z : 96 / Symbol : Cm / Name : キュリウム
Z : 116 / Symbol : Lv / Name : リバモリウム
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?