MongoDB in Action

#インストール
https://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/

##centos
http://qiita.com/khirose/items/14a2aea0a96564c9c74c

```
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten]
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten]
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten]
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2016-05-04T20:47:22.448+0900 I CONTROL  [initandlisten]

```

透過的hugepageについて(rhel6からの新機能)
アプリケーションに影響を与えずに、hugeoageを使うようにする設定？
http://blog.goo.ne.jp/u1low_cheap/e/72a7c5528858debe4e34df83430c480e
http://www.coins.tsukuba.ac.jp/~yas/coins/os2-2011/2012-01-24/index.html

理由:
沢山の細かいメモリアクセスをする場合、THPを不能にしたほうが、良いパフォーマンスが出る
http://stackoverflow.com/questions/29441679/why-does-mongodb-complain-about-transparent-hugepage

ソフトリミット・ハードリミット
https://docs.mongodb.com/manual/reference/ulimit/
https://linuxjm.osdn.jp/html/LDP_man-pages/man2/setrlimit.2.html

#起動と停止

```
~ $ mongod --dbpath mongodb/data/db/ --fork --logpath ~/mongolog

```

##停止
mongoで　dbに接続し、adminになったのち、db.shutdownServer()で停止をする。

```
~ $ mongod --config mongodb.config
about to fork child process, waiting until server is ready for connections.
forked process: 54084
child process started successfully, parent exiting
~ $ mongo
MongoDB shell version: 3.2.6
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2016-04-30T16:37:45.662+0900 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-04-30T16:37:45.662+0900 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2016-04-30T16:37:45.662+0900 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
```

#設定
##YML形式のコンフィグファイル
参考: https://docs.mongodb.org/manual/reference/configuration-options/

```yaml:mongodb.config
storage:
  dbPath: "/Users/dolin/mongodb/data/db/"
  journal:
    enabled: true
systemLog:
  destination: file
  path: "/Users/dolin/mongodb/mongod.log"
  logAppend: true
processManagement:
  fork: true
```

#Journal機能
参考:https://docs.mongodb.org/manual/tutorial/manage-journaling/
write ahead logging により、書き込み耐久性を保持。

#基本コマンド
##db一覧

```
> show dbs
local  0.000GB

> db.serverStatus().storageEngine
{
        "name" : "wiredTiger",
        "supportsCommittedReads" : true,
        "persistent" : true
}
```
#Index作成

```js:実行計画確認
> db.col.find({number:2000}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "test.col",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"number" : {
				"$eq" : 2000
			}
		},
		"winningPlan" : {
			"stage" : "COLLSCAN",
			"filter" : {
				"number" : {
					"$eq" : 2000
				}
			},
			"direction" : "forward"
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "A-no-MacBook-Air.local",
		"port" : 27017,
		"version" : "3.2.6",
		"gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25"
	},
	"ok" : 1
}
```

```js:index作成
> db.col.ensureIndex( { number : 1 } )
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

```

```js:再度実行計画確認
> db.col.find({number:2000}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "test.col",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"number" : {
				"$eq" : 2000
			}
		},
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "IXSCAN",
				"keyPattern" : {
					"number" : 1
				},
				"indexName" : "number_1",
				"isMultiKey" : false,
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 1,
				"direction" : "forward",
				"indexBounds" : {
					"number" : [
						"[2000.0, 2000.0]"
					]
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "A-no-MacBook-Air.local",
		"port" : 27017,
		"version" : "3.2.6",
		"gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25"
	},
	"ok" : 1
}
```



```
config = {
        "_id" : "rs1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27018"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27019"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27017"
                }
        ]
}
```