mongodbメモ

[参考](http://blog.bot.vc/2012/12/mongodb/)
[SQLとの対比](http://taka512.hatenablog.com/entry/20110220/1298195574)

ファイル
----

* /etc/mongodb.conf
* /etc/init.d/mongodb
* /var/log/mongodb/mongodb.log

mongostat
----

```bash:毎秒表示
$ mongostat 
connected to: 127.0.0.1
insert  query update delete getmore command flushes mapped  vsize    res faults  locked db idx miss %     qr|qw   ar|aw  netIn netOut  conn       time 
     0      0      0      0       0       1       0    80m   347m    33m      0  test:0.0%          0       0|0     0|0    62b     1k     1   15:48:17 
     0      0      0      0       0       1       0    80m   347m    33m      0  test:0.0%          0       0|0     0|0    62b     1k     1   15:48:18 
     0      0      0      0       0       1       0    80m   347m    33m      0  test:0.0%          0       0|0     0|0    62b     1k     1   15:48:19 
     0      0      0      0       0       1       0    80m   347m    33m      0  test:0.0%          0       0|0     0|0    62b     1k     
```

コンソール
----

```bash:
> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
> show dbs
local   (empty)
> use local
switched to db local
> db.local.find()
```

```bash:
> use test
switched to db test
> db.col.save({A:1});
> db.col.find();
{ "_id" : ObjectId("52146053f1cf872b926ddab2"), "A" : 1 }
> db.col.count();
1
> db.col.save({B:2});
> db.col.save({C:5});
> db.col.find();
{ "_id" : ObjectId("52146053f1cf872b926ddab2"), "A" : 1 }
{ "_id" : ObjectId("5214606cf1cf872b926ddab3"), "B" : 2 }
{ "_id" : ObjectId("5214606ff1cf872b926ddab4"), "C" : 5 }
> db.col.find({B:2});
{ "_id" : ObjectId("5214606cf1cf872b926ddab3"), "B" : 2 }
> db.col.remove({"C":5})
> db.col.find();
{ "_id" : ObjectId("52146053f1cf872b926ddab2"), "A" : 1 }
{ "_id" : ObjectId("5214606cf1cf872b926ddab3"), "B" : 2 }
> db.col.save({ "first_name" : "Jane", "last_name" : "Doe", "age" : 23  });
> db.col.find();
{ "_id" : ObjectId("52146053f1cf872b926ddab2"), "A" : 1 }
{ "_id" : ObjectId("5214606cf1cf872b926ddab3"), "B" : 2 }
{ "_id" : ObjectId("521460a6f1cf872b926ddab5"), "first_name" : "Jane", "last_name" : "Doe", "age" : 23 }
```