読者です 読者をやめる 読者になる 読者になる

@thorikiriのてょりっき

@thorikiriがWebとかAndroidとかの技術ネタや本を読んだブログです

今更MongoDBを使ってみたよ

database mongodb

環境はWindows7 32bitな。理由は今目の前にあったからだ。
Downloads - MongoDBからダウンロードしてきて、解凍したら、適当なディレクトリに置く。
そしたら、mongod.exe をコマンドラインから叩いてみよう。

D:\xxxxxxxx\data\mongodb-win32-i386-2.0.4\bin>mongod.exe
mongod.exe --help for help and startup options
Wed Apr 11 12:34:34
Wed Apr 11 12:34:34 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Wed Apr 11 12:34:34
Wed Apr 11 12:34:34 [initandlisten] MongoDB starting : pid=6676 port=27017 dbpath=/data/db 32-bit host=XXXXXX
Wed Apr 11 12:34:34 [initandlisten]
Wed Apr 11 12:34:34 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Wed Apr 11 12:34:34 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Wed Apr 11 12:34:34 [initandlisten] ** with --journal, the limit is lower
Wed Apr 11 12:34:34 [initandlisten]
Wed Apr 11 12:34:34 [initandlisten] db version v2.0.4, pdfile version 4.5
Wed Apr 11 12:34:34 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebf
Wed Apr 11 12:34:34 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_42
Wed Apr 11 12:34:34 [initandlisten] options: {}
Wed Apr 11 12:34:34 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db) does not exist, terminating
Wed Apr 11 12:34:34 dbexit:
Wed Apr 11 12:34:34 [initandlisten] shutdown: going to close listening sockets...
Wed Apr 11 12:34:34 [initandlisten] shutdown: going to flush diaglog...
Wed Apr 11 12:34:34 [initandlisten] shutdown: going to close sockets...
Wed Apr 11 12:34:34 [initandlisten] shutdown: waiting for fs preallocator...
Wed Apr 11 12:34:34 [initandlisten] shutdown: closing all files...
Wed Apr 11 12:34:34 [initandlisten] closeAllFiles() finished
Wed Apr 11 12:34:34 dbexit: really exiting now

エラーが出て終了しちゃった。どうやらデフォルトではデータベースのパスが /data/db になっているようだ。
たぶん、オプションで指定できるから、 mongod.exe --help でヘルプを見ると、

--dbpath arg directory for datafiles

ってなってるので、ここでDBのパスを指定すれば良いんだね。じゃあ、実行だ。

D:\xxxxxxxx\data\mongodb-win32-i386-2.0.4\bin>mongod.exe --dbpath D:\xxxxxxxx\data\db
Wed Apr 11 12:36:54
Wed Apr 11 12:36:54 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Wed Apr 11 12:36:54
Wed Apr 11 12:36:54 [initandlisten] MongoDB starting : pid=904 port=27017 dbpath=D:\xxxxxxxx\data\db 32-bit host=XXXXXX
Wed Apr 11 12:36:54 [initandlisten]
Wed Apr 11 12:36:54 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Wed Apr 11 12:36:54 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Wed Apr 11 12:36:54 [initandlisten] ** with --journal, the limit is lower
Wed Apr 11 12:36:54 [initandlisten]
Wed Apr 11 12:36:54 [initandlisten] db version v2.0.4, pdfile version 4.5
Wed Apr 11 12:36:54 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebf
Wed Apr 11 12:36:54 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_42
Wed Apr 11 12:36:54 [initandlisten] options: { dbpath: "D:\Projects\data\db" }
Wed Apr 11 12:36:54 [initandlisten] waiting for connections on port 27017
Wed Apr 11 12:36:54 [websvr] admin web console waiting for connections on port 28017
Wed Apr 11 12:37:35 [initandlisten] connection accepted from 127.0.0.1:53660 #1
Wed Apr 11 12:37:54 [clientcursormon] mem (MB) res:16 virt:71 mapped:0

起動したようだね。接続には、mongo.exeを使うよ。
基本的なコマンドはこんな感じ。

  • db.things.save(json); ドキュメントを突っ込む
  • db.things.find(); ドキュメントを探す
  • db.things.remove(json); ドキュメントを削除する
  • db.things.update(orgJson, newJson, upsert, multiple); ドキュメントを更新する。upsertがtrueの場合、存在してれば更新存在して無ければ登録。multipleがfalseの場合、最初に見つかったデータのみが更新される

D:\xxxxxxxx\data\mongodb-win32-i386-2.0.4\bin>mongo
MongoDB shell version: 2.0.4
connecting to: test
> db.things.save({"name":"thorikiri","age":32});
> db.things.save({"name":"thorikiri2","age":30});
> db.things.find();
{ "_id" : ObjectId("4f84fcafdca51a47a8a2b567"), "name" : "thorikiri", "age" : 32 }
{ "_id" : ObjectId("4f84fcd4dca51a47a8a2b568"), "name" : "thorikiri2", "age" : 30 }
> db.things.remove({"name":"thorikiri2"});
> db.things.find();
{ "_id" : ObjectId("4f84fcafdca51a47a8a2b567"), "name" : "thorikiri", "age" : 32 }
> db.things.update({"name":"thorikiri"}, {"name":"thorikiri", "age":17}, false, false);
> db.things.find();
{ "_id" : ObjectId("4f84fcafdca51a47a8a2b567"), "name" : "thorikiri", "age" : 17 }
> exit
bye

ね、簡単でしょう?

Document Design for Mongodb

Document Design for Mongodb

MongoDBに特化した日本語書籍は無いのかな?英語版はOreillyからいくつか出てるようですね。