DRYな備忘録

Don't Repeat Yourself.

MacでMongoDBのインストールとデーモン化と、基本的な使い方

インストール

まあbrew install mongodbですわ。

% brew search mongodb
mongodb

ok

% brew install mongodb
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mongodb-2.6.5.yosemite.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-2.6.5.yosemite.bottle.2.tar.gz
==> Caveats
To have launchd start mongodb at login:
    ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don\'t want/need launchctl, you can just run:
    mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺  /usr/local/Cellar/mongodb/2.6.5: 17 files, 331M

おしまい

Mac起動時にデーモンが立つようにしとく

上記のインストールログにあるように、

% ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
/Users/otiai10/Library/LaunchAgents/homebrew.mxcl.mongodb.plist -> /usr/local/opt/mongodb/homebrew.mxcl.mongodb.plist

今すぐデーモンを立たせる

上記のインストールログにあるように、

% launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

ログインしてみる

クライアントコンソールに入る。Just hit mongo !

% mongo
MongoDB shell version: 2.6.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
>

おk。いけてるので、いちど抜ける

> exit
bye

せっかくなので、基本的なコマンドの使い方紹介

> show dbs
admin  (empty)
local  0.078GB

デフォルトでconnecting to: testって言われたのに、なんでここにtest databaseが無いのかというと、

MongoDB will not permanently create a database until you insert data into that database.

Getting Started with MongoDB — MongoDB Manual 2.6.4

何かしら保存すべきデータが発生したときにはじめて、データベースはつくられます。

> use mydb
switched to db mydb
> db.createCollection("myitems")
{ "ok" : 1 }
# 第二引数にコレクションの設定を指定可能

insert

> db.myitems.insert({hoge:true, fuga:1000, piyo:{foo:"This is foo", bar: new Date()}})
WriteResult({ "nInserted" : 1 })

find

> db.myitems.findOne()
{
    "_id" : ObjectId("547dbce406f46d1dcd54980e"),
    "hoge" : true,
    "fuga" : 1000,
    "piyo" : {
        "foo" : "This is foo",
        "bar" : ISODate("2014-12-02T13:21:40.844Z")
    }
}

queryでfind

> db.myitems.find({_id: ObjectId("547dbce406f46d1dcd54980e")}).pretty()
{
    "_id" : ObjectId("547dbce406f46d1dcd54980e"),
    "hoge" : true,
    "fuga" : 1000,
    "piyo" : {
        "foo" : "This is foo",
        "bar" : ISODate("2014-12-02T13:21:40.844Z")
    }
}

検索結果に対してupdate

> db.myitems.update({_id: ObjectId("547dbce406f46d1dcd54980e")}, {$set:{"piyo.bar": new Date()}, $inc: {fuga: 2}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.myitems.findOne();
{
    "_id" : ObjectId("547dbce406f46d1dcd54980e"),
    "hoge" : true,
    "fuga" : 1002,
    "piyo" : {
        "foo" : "This is foo",
        "bar" : ISODate("2014-12-02T13:29:50.941Z")
    }
}
# 第二引数にドキュメントそのものをセットすれば全体更新
# {$set:...}などのOperationキーワードを渡せば部分更新などできる

検索結果に対してdelete

# たとえば、"piyo.bar"が今より小さいドキュメントは全部削除
> db.myitems.remove({"piyo.bar":{$lt: new Date()}})
WriteResult({ "nRemoved" : 1 })
> db.myitems.find()
>

だいたい基本的な使い方でした。

路傍の石的な備忘録

DRY