惭辞苍驳辞顿叠の使い方4. MongoDB source
Linux Sever latest var (x.y.z = 2.2.2)
64bit
→ curl http://fastdl.mongodb.org/linux/mongodb-linux-
x86_64-2.2.2.tgz > mongo.tgz
32bit
→ curl hhttp://fastdl.mongodb.org/linux/mongodb-linux-
i686-2.2.2.tgz > mongo.tgz
5. MongoDB install
解凍する
→ tar -zxvf mongo.tgz
適当な場所に置く
→ ln -s /usr/src/mongodb-linux-i686-2.0.6 /usr/local/mongo
コマンドで使えるようPathを通す
→ PATH=$PATH:/usr/local/mongo/bin
7. MongoDB sever
サーバーの起動
→ mongod --config /usr/local/mongo/conf/mongodb.config
aliasを登録しておくと便利
alias mongod='mongod --config /usr/local/mongo/conf/
mongodb.config'
サーバーを起動したら、別のターミナルで
→ mongo
8. MongoDB
データベースを見る
show dbs
MySQL → show databases
データベースを使う(作る)
use comedy
MySQL → use comedy & create database comedy
コレクション(テーブル)を見る
show collections
MySQL → show tables
11. MongoDB
取得結果
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c2"), "name" : "NARUTO", "book_number" : 1 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c3"), "name" : "NARUTO", "book_number" : 2 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c4"), "name" : "NARUTO", "book_number" : 3 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c5"), "name" : "NARUTO", "book_number" : 4 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c6"), "name" : "NARUTO", "book_number" : 5 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c7"), "name" : "NARUTO", "book_number" : 6 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c8"), "name" : "NARUTO", "book_number" : 7 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428c9"), "name" : "NARUTO", "book_number" : 8 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428ca"), "name" : "NARUTO", "book_number" : 9 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428cb"), "name" : "NARUTO", "book_number" : 10 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428cc"), "name" : "NARUTO", "book_number" : 11 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428cd"), "name" : "NARUTO", "book_number" : 12 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428ce"), "name" : "NARUTO", "book_number" : 13 }
{ "_id" : ObjectId("4fe6fd26540eb3b96a6428cf"), "name" : "NARUTO", "book_number" : 14 }
13. MongoDB driver
<?php
// MongoDB接続
// デフォルトの設定であれば new MongoでOK
// mongod を --auth or --keyFile で起動していた場合
$m = new Mongo("mongodb://${username}:${password}
@localhost");
//database選択
$db = $m->comedy; //comedy というDBを選択
//collection選択
$collection = $db->cartoons; //cartoosというコレクションを選択
14. MongoDB driver
//データ挿入
$obj_insert = array( "name" => "NARUTO", "number" => 13 );
$collection->insert($obj_insert);
//データ更新
$obj_where = array( "name" => "NARUTO" );
$obj_set = array( "number" => 14 );
$collection->update($obj_where,$obj_set);
//データ消去
$obj_remove = array( "name" => "NARUTO" );
$collection->remove($obj_remo);
15. MongoDB driver
//データ取得
$cursor = $collection->find("name" => "NARUTO");
foreach ($cursor as $obj) {
echo $obj["name"]. "n";
}
?>
結果(name=”NARUTO” が 10あった場合)
NARUTO NARUTO NARUTO NARUTO NARUTO NARUTO
NARUTO NARUTO NARUTO NARUTO