MongoDBでらくしようと思い立ちました。
インストール
takuya@air:~$ brew install mongodb ==> Downloading http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.2.tgz ######################################################################## 100.0% ==> Caveats If this is your first install, automatically load on login with: mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/mongodb/2.0.2-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist If this is an upgrade and you already have the org.mongodb.mongod.plist loaded: launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist cp /usr/local/Cellar/mongodb/2.0.2-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist Or start it manually: mongod run --config /usr/local/Cellar/mongodb/2.0.2-x86_64/mongod.conf MongoDB 1.8+ includes a feature for Write Ahead Logging (Journaling), which has been enabled by default. To disable journaling, use --nojournal. ==> Summary /usr/local/Cellar/mongodb/2.0.2-x86_64: 18 files, 121M, built in 2.6 minutes
起動
mongod run --config /usr/local/Cellar/mongodb/2.0.2-x86_64/mongod.conf
設定ファイルについて
1 # Store data in /usr/local/var/mongodb instead of the default /data/db 2 dbpath = /usr/local/var/mongodb 3 4 # Only accept local connections 5 bind_ip = 127.0.0.1
接続について
takuya@air:~$ mongo MongoDB shell version: 2.0.6 connecting to: test >
データベースファイルについて
takuya@air:~$ find /usr/local/var/mongodb/ -exec ls -l {} + -rwxr-xr-x 1 takuya admin 0 12 31 22:36 /usr/local/var/mongodb/mongod.lock -rw------- 1 takuya admin 67108864 12 31 17:01 /usr/local/var/mongodb/test.0 -rw------- 1 takuya admin 134217728 12 31 16:34 /usr/local/var/mongodb/test.1 -rw------- 1 takuya admin 16777216 12 31 17:01 /usr/local/var/mongodb/test.ns /usr/local/var/mongodb/: total 212992 drwxr-xr-x 2 takuya admin 68 12 31 17:00 _tmp drwxr-xr-x 2 takuya admin 68 12 31 22:36 journal -rwxr-xr-x 1 takuya admin 0 12 31 22:36 mongod.lock -rw------- 1 takuya admin 67108864 12 31 17:01 test.0 -rw------- 1 takuya admin 134217728 12 31 16:34 test.1 -rw------- 1 takuya admin 16777216 12 31 17:01 test.ns /usr/local/var/mongodb/_tmp: total 0 /usr/local/var/mongodb/journal: total 0
各ファイルについて
testはコレクション
db.users.save( { name:"takuya"} )
これは次の意味になります。
・データベースに
・user コレクションを作って
・name というキーを持つドキュメントを保存
使い方基本
db.users.save({name: 'moro', tags: ['ruby', 'rails'], login_count: {today: 5, total: 25}}) #↑db.users.save( <JSON> ) db.users.find() # ↑ find 関数 サンプル > db.users.save( { name:"takuya"} ) > db.users.find() { "_id" : ObjectId("508cb3c882c00c9e84281020"), "name" : "takuya" } > bye
TIPS
mongoシェルはSpiderMonkeyシェルの拡張