# query

## 操作系

### 終了

> use admin
switched to db admin
> db.shutdownServer()

### DB一覧取得

> show dbs
local   (empty)
test002 0.203125GB
testdb  (empty)

### DB削除

> show dbs
local   (empty)
test002 0.203125GB
testdb  (empty)
> use test002
switched to db test002
> db.dropDatabase()
{ "dropped" : "test002", "ok" : 1 }
> show dbs
local   (empty)
testdb  (empty)

### コレクション削除

>db.[コレクション名].drop()

### カレントDBを確認

> db
testdb

### カレントDBのコレクション一覧取得

> show collections
system.indexes
testcol
testdata

### 最適化(※db容量と同等の空き容量がシステムに必要)

> use test002
switched to db test002
> db.repairDatabase()

### キーのリネーム
http://docs.mongodb.org/manual/core/update/#Updating-%24rename

> db.test002.update({},{$rename:{"oldname":"newname"}},false,true);

## 一般

> db.コレクション名.処理種別( { 検索条件 } , { 取得項目 }  )
select
    取得カラム(= 取得項目)
from
    テーブル名(= コレクション名)
where
    検索条件

### コレクション内のレコード数取得

> db.testdata.count()
150

### コレクション内の先頭1件取得

> db.testdata.findOne()
{
        "_id" : ObjectId("5109be7579ee8df58e8feca6"),
        "entryId" : 1,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.080Z"),
        "name" : "test01"
}

### コレクション内のentryId=“99”のレコード取得

> db.testdata.find({entryId:"99"})
⇒型が違うので、該当なし

### コレクション内のentryId=99のレコード取得

> db.testdata.find({entryId:99})
{ "_id" : ObjectId("5109be7579ee8df58e8fed08"), "entryId" : 99, "createdDatetime" : ISODate("2013-01-31T00:44:37.086Z"), "name" : "test099" }

### コレクション内のentryId=99のレコード件数取得

> db.testdata.count({entryId:99})
1

### コレクション内のentryId>99のレコード取得

> db.testdata.count( { entryId: { $gt: 99 } } )
51

### コレクション内の99⇐entryId<102のレコード件数取得

> db.testdata.count( { entryId: { $gte: 99, $lt: 102 } } )
3

### コレクション内の99⇐entryId<102のレコードをentryIdの降順に取得

> db.testdata.find( { entryId: { $gte: 99, $lt: 102 } } ).sort({entryId:-1})
{ "_id" : ObjectId("5109be7579ee8df58e8fed0a"), "entryId" : 101, "createdDatetime" : ISODate("2013-01-31T00:44:37.086Z"), "name" : "test0101" }
{ "_id" : ObjectId("5109be7579ee8df58e8fed09"), "entryId" : 100, "createdDatetime" : ISODate("2013-01-31T00:44:37.086Z"), "name" : "test0100" }
{ "_id" : ObjectId("5109be7579ee8df58e8fed08"), "entryId" : 99, "createdDatetime" : ISODate("2013-01-31T00:44:37.086Z"), "name" : "test099" }

### コレクション内の99⇐entryId<102のレコードをentryIdの降順に取得して、整形して表示

> db.testdata.find({entryId:{$gte:99,$lt:102}}).sort({entryId:-1}).forEach(printjson)
{
        "_id" : ObjectId("5109be7579ee8df58e8fed0a"),
        "entryId" : 101,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.086Z"),
        "name" : "test0101"
}
{
        "_id" : ObjectId("5109be7579ee8df58e8fed09"),
        "entryId" : 100,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.086Z"),
        "name" : "test0100"
}
{
        "_id" : ObjectId("5109be7579ee8df58e8fed08"),
        "entryId" : 99,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.086Z"),
        "name" : "test099"
}

### コレクション内の99⇐entryId<110のレコードをentryIdの降順に取得して、3件を整形して表示

> db.testdata.find({entryId:{$gte:99,$lt:110}}).sort({entryId:-1}).limit(3).forEach(printjson)
{
        "_id" : ObjectId("5109be7579ee8df58e8fed12"),
        "entryId" : 109,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.087Z"),
        "name" : "test0109"
}
{
        "_id" : ObjectId("5109be7579ee8df58e8fed11"),
        "entryId" : 108,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.087Z"),
        "name" : "test0108"
}
{
        "_id" : ObjectId("5109be7579ee8df58e8fed10"),
        "entryId" : 107,
        "createdDatetime" : ISODate("2013-01-31T00:44:37.087Z"),
        "name" : "test0107"
}
>

### コレクション内の2013-05-07 13時までのレコード件数取得

> db.testdata.find({time: {$gte: ISODate("2013-05-07T13:00:00+09:00")}}).count();

### like検索(正規表現)i:大文字小文字を同一視

> db.access.find({referer: /192.168.26.141/i})

### 特定項目(以下の例ではreferer)のみ取得

> db.access.find({}, {referer:1})
{ "_id" : ObjectId("510b0daeddfe4129a7000002"), "referer" : "http://192.168.26.143/pma/" }

### 登録・更新

> db.collect.save( { a : 1 } )
</code>

### 削除

> db.collect.remove( { a : 1 } )

### sql対比

{ "a": "b" }
SELECT * FROM test WHERE a = 'b'
{ "a": { "$ne"    : "b" } }
SELECT * FROM test WHERE a != 'b'
{ "a": { "$in"    : [ "b", "c" ] } }
SELECT * FROM test WHERE a IN ('b', 'c')
{ "a": { "$nin"   : [ "b", "c" ] } }
SELECT * FROM test WHERE a NOT IN ('b', 'c')
{ "a": { "$exists": 0 } }
SELECT * FROM test WHERE a IS NULL
{ "a": { "$exists": 1 } }
SELECT * FROM test WHERE a IS NOT NULL
{ "a": { "$lt"    : 1 } }
SELECT * FROM test WHERE a < '1'
{ "a": { "$gt"    : 2 } }
SELECT * FROM test WHERE a > '2'
{ "a": { "$lte"   : 3 } }
SELECT * FROM test WHERE a <= '3'
{ "a": { "$gte"   : 4 } }
SELECT * FROM test WHERE a >= '4'
{ "a": "b", "c": "d" }
SELECT * FROM test WHERE (c = 'd' AND a = 'b')
{ "$and": [ { "a": "b" }, { "a": "d" } ] }
SELECT * FROM test WHERE (a = 'b' AND a = 'd')
{ "$or": [ { "a": "b" }, { "c": "d" } ] }
SELECT * FROM test WHERE (a = 'b' OR c = 'd')
{ "$or": [ { "a": "b", "c": "d" }, { "e": "f" } ] }
SELECT * FROM test WHERE ((c = 'd' AND a = 'b') OR e = 'f')
{ "a": "b", "$or": [ { "c": "d" }, { "e": "f" } ] }
SELECT * FROM test WHERE (a = 'b' AND (c = 'd' OR e = 'f'))
{ "$or": [ { "a": "b", "c": "d" }, { "e": "f", "g": "h" } ] }
SELECT * FROM test WHERE ((c = 'd' AND a = 'b') OR (e = 'f' AND g = 'h'))
{ "$and": [ { "$or": [ { "a": "b" }, { "c": "d" } ] }, { "$or" : [ { "e": "f" }, { "g": "h" } ] } ] }
SELECT * FROM test WHERE ((a = 'b' OR c = 'd') AND (e = 'f' OR g = 'h'))