Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MongoDBの使い方

4,040 views

Published on

  • Be the first to comment

MongoDBの使い方

  1. 1. MongoDB ∼使い方∼
  2. 2. MongoDBスキーマレス alter tableなどでの変更いらないドキュメントがBSON( JSON )データ型も自由に決める
  3. 3. MongoDB sourcehttp://www.mongodb.org/downloads
  4. 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.tgz32bit → curl hhttp://fastdl.mongodb.org/linux/mongodb-linux- i686-2.2.2.tgz > mongo.tgz
  5. 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
  6. 6. MongoDB databaseDBの場所を決める フォルダを作る→ /usr/local/mongo/dataconfigファイルを作る。場所はどこでもいいmongodb.config を作成ファイルに以下の記述をする→ dbpath=/usr/local/mongo/data/
  7. 7. MongoDB severサーバーの起動→ mongod --config /usr/local/mongo/conf/mongodb.configaliasを登録しておくと便利alias mongod=mongod --config /usr/local/mongo/conf/mongodb.configサーバーを起動したら、別のターミナルで→ mongo
  8. 8. MongoDBデータベースを見るshow dbsMySQL → show databasesデータベースを使う(作る)use comedyMySQL → use comedy & create database comedyコレクション(テーブル)を見るshow collectionsMySQL → show tables
  9. 9. MongoDB挿入db.cartoons.insert({name:"NARUTO",number:13});db.cartoons.save({name:"NARUTO",number:13});更新db.cartoons.update({name:"NARUTO"},{$set: {number: 14}});cartoon = db.cartoons.findOne( { name : "NARUTO" } );cartoon.number = 14;db.cartoons.save( cartoon );
  10. 10. MongoDB消去コレクションの消去db.cartoons.drop();ドキュメントの消去db.cartoons.remove();db.cartoons.remove({name:"NARUTO"});取得db.cartoons.find();db.cartoons.find({name:"NARUTO"});
  11. 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 }
  12. 12. MongoDB driver公式のドライバhttp://www.mongodb.org/display/DOCS/DriversphpでmongoDB→ sudo pecl install mongophp.ini に以下を記述extension=mongo.so
  13. 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. 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. 15. MongoDB driver//データ取得$cursor = $collection->find("name" => "NARUTO");foreach ($cursor as $obj) { echo $obj["name"]. "n";}?>結果(name=”NARUTO” が 10あった場合)NARUTO NARUTO NARUTO NARUTO NARUTO NARUTONARUTO NARUTO NARUTO NARUTO

×