LoginSignup
8
10

More than 5 years have passed since last update.

mongoDBのあれこれ

Last updated at Posted at 2015-10-01

MongoDBのあれこれのメモ

mongoDBをイジる上で備忘録的に追記していく。

(1)ログイン

mongoでのDB選択
% mongo                                                                                                  MongoDB shell version: 2.6.1
connecting to: test
> use hoge

これでクライアントを起動しDBの選択が出来る。
ちなみに起動時にDBを選択することも可能。

mongoでのDB選択
% mongo hoge
MongoDB shell version: 2.6.1
connecting to: hoge
>

設定ファイルを書き換えるとリモートのアドレスを指定することも可能。
bind_ipで制限されているのでコメントアウトする。

mongo.conf変更前
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=127.0.0.1
mongo.conf変更後
# Listen to local interface only. Comment out to listen on all interfaces.
# bind_ip=127.0.0.1
mongoでのリモートログイン
% mongo example.com:27017/hoge
MongoDB shell version: 2.6.1
connecting to: hoge
>

リモートでログインできない場合はポート番号があっているかとかそもそもポートが開いていない場合があるので確認を。

IPアドレスベースでのアクセス制限が出来ない(するほうほうがあれば教えて欲しい)ので、iptablesで制限するかmongoDBの設定でauthで認証をかける。

mongo.conf変更後
# Turn on/off security.  Off is currently the default
#noauth=true
auth=true

次にDB毎に権限を付与してログインしてみる。

mongoでのDB
% mongo
> use admin
> db.createUser({ user: "admin", pwd: "adminpass", roles: ["userAdminAnyDatabase"] })

% mongo admin -u admin -p adminpass
> use hoge
> db.createUser({ user: "hoge_user", pwd: "hogehoge", roles: ["readWrite"] })

ip_bindをコメントアウトしてauthを設定していればこれでいける

mongoでのリモートログイン
% mongo example.com:27017/hoge -u hoge_user -p hogehoge 
MongoDB shell version: 2.6.1
connecting to: hoge
>

roleで設定してある権限は以下を参照。
createRoleで新しい権限も作れるらしい(試してない)
mongoDB - Manage User and Roles

(2)mongoDBでの検索

そもそものMySQLとmongoDBの対応表らしきものを

MySQL mongoDB
DB DB
table collection
record document
column field

基本的にDBやcollectionは作らなくてもinsertするときに作られます。
MySQLでのレコードでは同じテーブル上であえば同じカラムですが、mongoDBでは自由にfieldを決めてinsert出来ます。

条件なし

> db.hoge_col.find()
まずこれが基本形で、MySQLだとこんな感じ。
select * from hoge.hoge_col;

1件だけ取得

> db.hoge_col.find().limit(1)

主キー検索:idがabcdefgのものを取得

> db.hoge_col.find({"_id": ObjectId("abcdefg")})

基本的な条件検索:nameがhogeのものを取得

> db.hoge_col.find({"name": "hoge"})

ノットイコール(≠):nameがhogeではないものを取得

> db.hoge_col.find({"name": {$ne: "hoge"}})

カウント:nameがhogeのドキュメント数を取得

> db.hoge_col.find({"name": "hoge"}).count()

>=での検索:scoreが80以上ものを取得

> db.hoge_col.find({"score": {$gte: 80}})

>での検索:scoreが80より大きいものを取得

> db.hoge_col.find({"score": {$gt: 80}})

<=での検索:scoreが80以下ものを取得

> db.hoge_col.find({"score": {$lte: 80}})

<での検索:scoreが80より小さいものを取得

> db.hoge_col.find({"score": {$lt: 80}})

AND条件:nameがhogeかつscoreが80以上ものを取得

> db.hoge_col.find({$and: [{"name": "hoge"}, {"socre": {$gte: 80}}]}))

OR条件:nameがhogeまたはscoreが80以上ものを取得

> db.hoge_col.find({$or: [{"name": "hoge"}, {"socre": {$gte: 80}}]}))

ユニーク条件:nameフィールドにdistictの条件をつけて検索

> db.hoge_col.distinct({"name"})

ユニーク条件に検索条件を付ける:scoreが80以上のnameフィールドにdistinctの条件をつけて検索

> db.hoge_col.distinct("name", {"socre": {$gte: 80}})

ユニーク条件でのカウント:nameフィールドにdistictの条件をつけてカウント

countではなくlength
> db.hoge_col.distinct("name").length

(3)スロークエリの調査&インデックス

後日

(4)バックアップ&転送

後日

参考

http://dqn.sakusakutto.jp/2013/03/mongodb_migration_merge.html
http://qiita.com/hiromisawada/items/ec266e423d7b273cd239
http://tweeeety.hateblo.jp/entry/20140311/1394528997
http://kozy4324.github.io/blog/2012/06/19/mongodb-index/

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10