notebook

都内でWEB系エンジニアやってます。

mongoDBを使ってみる

mongoDBを使ってみる

インストール

  • /etc/yum.repo.d/10gen.reop
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0
enabled=1
  • インストール
yum install mongo-10gen mongo-10gen-server
  • 起動
/etc/init.d/mongod start
mongod --version
  • シェルの起動
$ mongo
# DB一覧
> show dbs
# 使用するDBを選択
> use aaa (存在していなくてもOK)
# コレクション一覧
> show collections
# 現在使用しているDB
> db
aaa
# insert
> db.test.insert({ "key1": "value1", "key2": "value2" })
# 全件取得
> db.test.find()
# 条件付き取得
> db.test.find({"key1": "value1"})
{ "_id" : ObjectId("54feffaabb8ce4ca0f6b990e"), "key1" : "value1", "key2" : "value2" }
> db.test.find({"key2": "value2"})
{ "_id" : ObjectId("54feffaabb8ce4ca0f6b990e"), "key1" : "value1", "key2" : "value2" }
# keyの指定(第2引数で返却するkeyを指定できる)
> db.test.find(null, {key3: 1})
{ "_id" : ObjectId("54feffaabb8ce4ca0f6b990e") }
{ "_id" : ObjectId("54ff002cbb8ce4ca0f6b990f"), "key3" : "value3" }
# 1件取得
> db.test.findOne()

# 更新
> db.test.update({"key3": "value3"}, {settest: true})
> db.test.find()
{ "_id" : ObjectId("54ff002cbb8ce4ca0f6b990f"), "settest" : true }
# $setで特定のkeyを更新することができる
> db.test.update({"key3": "value3"},{$set: {settest: true}})
> db.test.find()
{ "_id" : ObjectId("54ff002cbb8ce4ca0f6b990f"), "key3" : "value3", "key4" : "value4", "settest" : true }
# 数値計算
> db.test.insert({"key": "count", "count": 1})
{ "_id" : ObjectId("54ff06bcbb8ce4ca0f6b9912"), "count" : 1, "key" : "count" }
> db.test.update({key: "count"}, {$inc: {count: -1}})
{ "_id" : ObjectId("54ff06bcbb8ce4ca0f6b9912"), "count" : 0, "key" : "count" }
# update or insert
> db.test.update({"key": "test"}, {$inc: {count: 1}}, true)
> db.test.find()
{ "_id" : ObjectId("54ff08bdb68567dff13a9a8d"), "key" : "test", "count" : 1 }
> db.test.update({"key": "test"}, {$inc: {count: 1}}, true)
{ "_id" : ObjectId("54ff08bdb68567dff13a9a8d"), "key" : "test", "count" : 2 }
# 複数更新(updateはデフォルトで最後に見つかったドキュメントのみを更新する)
db.test.update({},{$set: {"bulk": true}},false,true)
  • 入れ子
# keyを.でつなげて指定ができる
> db.test.insert({ name: "Taro", family: {mother: "Hanako", father: "Ichiro"}})
db.test.find({ 'family.mother': "Hanako"})
{ "_id" : ObjectId("54ff0fe8bb8ce4ca0f6b9913"), "name" : "Taro", "family" : { "mother" : "Hanako", "father" : "Ichiro" } }
# コレクションの情報を表示
> db.test.stats()
# 見やすいように成形
> db.test.find().pretty()

perlから使ってみる

ちょっと触ってみて面白くなってきたのでperlから使ってみました

#!/usr/bin/env perl

use strict;
use warnings;

use utf8;

use MongoDB;
use Data::Dumper;
use JSON;

my $client = MongoDB::MongoClient->new();
my $db = $client->get_database('perl_db');
my $collection = $db->get_collection('perl_collection');
# $collection->insert({ "key" => "value1"});
# $collection->update({ "key"=>"value"},{ '$set'=>{"name" => "taro"} } );
$collection->update({ "key" => "count"}, {'$inc' => {"count" => 2 }});

my $results = $collection->find();

while ( my $row = $results->next ) {
  print "ObjectId: " . $row->{ _id } . "\t";
  delete $row->{ _id };
  my $json = encode_json( $row );
  print $json . "\n";
}
$ perl mongo.pl
ObjectId: 54ff29e6e138232f91316fb1 {"key":"value1"}
ObjectId: 54ff2af2e13823430d0ac901 {"key":"value1"}
ObjectId: 54ff29bbe138230f647018c1 {"key":"value","name":"taro"}
ObjectId: 55003842e138236f7c3d91e1 {"key":"count","count":19}

とても簡単に実装できそう、取りあえずアンケートアプリでも作ろうか。。。