ドットインストールのMongoDB入門をやった時のメモです。
1. データベースに関する操作
1.1. DBの一覧を見たい
> show dbs
1.2. DBを作りたい
> use mydb
1.3. 今使っているDBを別のDBに切り替えたい
> use mydb
1.4. DBに関する全般的な情報を見たい
> db.stats()
{
"db" : "local",
"collections" : 1,
"objects" : 2,
"avgObjSize" : 1423,
"dataSize" : 2846,
"storageSize" : 32768,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 32768,
"ok" : 1
}
1.5. DBを削除したい
> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
2. コレクションに関する操作
2.1. Collectionの一覧を見たい
> show collections
users
2.2. Collectionを作りたい
> db.createCollection("users")
{ "ok" : 1 }
2.3. Collection名を変更したい
> db.users.renameCollection("customers")
{ "ok" : 1 }
2.4. Collectionを削除したい
> db.customers.drop()
true
3. Documentに関する操作
3.1. Create系操作
3.1.1. Documentを作りたい
> db.users.insert({name: "inaba", score: 100})
WriteResult({ "nInserted" : 1 })
> db.users.insert({name: "taguchi", score: 100, tags: ["web", "mobile"]})
WriteResult({ "nInserted" : 1 })
3.1.2. forループでDocumentを作りたい
> for (var i = 0; i < 10; i++) {
... db.users.insert({name: "inaba", score: Math.random()})
... }
WriteResult({ "nInserted" : 1 })
3.2. Read系操作
3.2.1. Documentの一覧を見たい
> db.users.find()
{ "_id" : ObjectId("56a620e956bad693a0943b6f"), "name" : "inaba", "score" : 100 }
{ "_id" : ObjectId("56a6212d56bad693a0943b70"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.2. Document数を知りたい
> db.users.count()
22
3.2.3. 条件を指定してDocumentを抽出したい
3.2.3.1. Equal / Not equal 条件
> db.users.find({name: "taguchi"})
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.find({score: {$eq: 0.7505969204990847}})
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
> db.users.find({score: {$ne: 0.7505969204990847}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.3.2. Less than / Less than equal / Greater than / Greater than equal 条件
> db.users.find({score: {$gte: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.find({score: {$lte: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
> db.users.find({score: {$lt: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
> db.users.find({score: {$gt: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.3.3. 正規表現で条件を指定する
> db.users.find({name: /^.+chi$/})
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.3.4. And条件で複数の条件を指定してDocumentを抽出したい
> db.users.find({name: /^i.+a$/, score: {$gte: 0.5}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
3.2.3.5. Or条件で複数の条件を指定してDocumentを抽出したい
> db.users.find({$or: [{name: /^i.+a$/}, {score: {$gte: 0.5}}]})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba", "score" : 0.05682618331619349 }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba", "score" : 0.8607307464013195 }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba", "score" : 0.7505969204990847 }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba", "score" : 0.6928152829564784 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba", "score" : 0.12730769462146607 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba", "score" : 0.7224980348563089 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba", "score" : 0.6146229528346954 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba", "score" : 0.6281710749925821 }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
3.2.3.6. IN句を使ってDocumentを抽出したい
> db.users.find({score: {$in: [0.696594451078292, 0.6896075949404165]}})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba", "score" : 0.696594451078292 }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba", "score" : 0.6896075949404165 }
3.2.3.7. ある特定のフィールドが存在するDocumentを抽出したい
> db.users.find({age: {$exists: true}})
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "name" : "tanaka", "score" : 52, "age" : 23 }
3.2.4. フィールドに入っている値の一覧を見たい
> db.users.distinct("name")
[ "inaba", "taguchi" ]
3.2.5. フィールドを指定してDocumentを抽出したい
> db.users.find({}, {name: true})
{ "_id" : ObjectId("56a625ec56bad693a0943b85"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b86"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b87"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b88"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b89"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8a"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8b"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8c"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8d"), "name" : "inaba" }
{ "_id" : ObjectId("56a625ec56bad693a0943b8e"), "name" : "inaba" }
{ "_id" : ObjectId("56a6261d56bad693a0943b8f"), "name" : "taguchi" }
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "name" : "tanaka" }
> db.users.find({age: {$exists: true}}, {name: 1})
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "name" : "tanaka" }
> db.users.find({age: {$exists: true}}, {name: 0})
{ "_id" : ObjectId("56a638ce56bad693a0943b90"), "score" : 52, "age" : 23 }
> db.users.find({age: {$exists: true}}, {name: 1, _id: 0})
{ "name" : "tanaka" }
3.2.6. 抽出結果をソートしたい
> db.users.find({}, {_id: false}).sort({score: 1})
{ "name" : "inaba", "score" : 0.05682618331619349 }
{ "name" : "inaba", "score" : 0.12730769462146607 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
{ "name" : "inaba", "score" : 0.6896075949404165 }
{ "name" : "inaba", "score" : 0.6928152829564784 }
{ "name" : "inaba", "score" : 0.696594451078292 }
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.7505969204990847 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.find({}, {_id: false}).sort({score: -1})
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
{ "name" : "inaba", "score" : 0.7505969204990847 }
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.696594451078292 }
{ "name" : "inaba", "score" : 0.6928152829564784 }
{ "name" : "inaba", "score" : 0.6896075949404165 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.12730769462146607 }
{ "name" : "inaba", "score" : 0.05682618331619349 }
3.2.7. 抽出結果の件数を絞りたい
3.2.7.1. 抽出結果のうち、最初のN件を抽出したい
> db.users.find({}, {_id: false}).sort({score: -1}).limit(3)
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
3.2.7.2. 抽出結果のうち、最初の一件のみ抽出したい
> db.users.findOne({}, {_id: 0})
{ "name" : "inaba", "score" : 0.696594451078292 }
3.2.7.3. 抽出結果のうち、最初のN件を飛ばして抽出したい
> db.users.find({}, {_id: 0}).skip(7)
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
{ "name" : "tanaka", "score" : 52, "age" : 23 }
3.3. Update系操作
3.3.1. 検索条件に一致する最初のDocumentの、特定のフィールドを更新したい
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi", "score" : 100, "tags" : [ "web", "mobile" ] }
> db.users.update({name: "taguchi"}, {$set: {score: 80}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi", "score" : 80, "tags" : [ "web", "mobile" ] }
> db.users.update({name: "taguchi"}, {$set: {score: 80, tag: "new tag"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi", "score" : 80, "tags" : [ "web", "mobile" ], "tag" : "new tag" }
3.3.2. 検索条件に一致する最初のDocumentの、Document全体を更新したい
> db.users.update({name: "taguchi"}, {name: "taguchi"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "taguchi"}, {_id: 0})
{ "name" : "taguchi" }
3.3.3. 検索条件に一致する全Documentの、特定のフィールドを更新したい
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 0.696594451078292 }
{ "name" : "inaba", "score" : 0.05682618331619349 }
{ "name" : "inaba", "score" : 0.8607307464013195 }
{ "name" : "inaba", "score" : 0.7505969204990847 }
{ "name" : "inaba", "score" : 0.6928152829564784 }
{ "name" : "inaba", "score" : 0.12730769462146607 }
{ "name" : "inaba", "score" : 0.6896075949404165 }
{ "name" : "inaba", "score" : 0.7224980348563089 }
{ "name" : "inaba", "score" : 0.6146229528346954 }
{ "name" : "inaba", "score" : 0.6281710749925821 }
> db.users.update({name: "inaba"}, {$set: {score: 80}}, {multi: true})
WriteResult({ "nMatched" : 10, "nUpserted" : 0, "nModified" : 10 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
3.3.4. 検索条件に一致する最初のDocumentの、特定のフィールドにNを加算したい
> db.users.update({name: "inaba"}, {$inc: {score: 5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
3.3.5. 検索条件に一致する最初のDocumentの、特定のフィールドにNを乗算したい
> db.users.update({name: "inaba"}, {$mul: {score: 5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "score" : 425 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
{ "name" : "inaba", "score" : 80 }
3.3.6. 検索条件に一致する最初のDocumentの、特定のフィールドの名前を変更したい
> db.users.update({name: "inaba"}, {$rename: {score: "point"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "point" : 2135 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
3.3.7. 検索条件に一致する最初のDocumentに、フィールドを追加したい
> db.users.update({name: "inaba"}, {$set: {team: "team-4"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "point" : 2135, "team" : "team-4" }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
3.3.8. 検索条件に一致する最初のDocumentの、フィールドを削除したい
> db.users.update({name: "inaba"}, {$unset: {point: ""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "inaba"}, {_id: 0})
{ "name" : "inaba", "team" : "team-4" }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
{ "name" : "inaba", "score" : 85 }
3.3.9. 検索条件に一致するDocumentがあれば、最初のDocumentを更新し、なければ新規Documentを作成したい
> db.users.find({name: "nakata"}, {_id: 0})
>
> db.users.update({name: "nakata"}, {name: "nakata", score: 99}, {upsert: true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("56a8b8358be74315594831c3")
})
> db.users.find({name: "nakata"}, {_id: 0})
{ "name" : "nakata", "score" : 99 }
> db.users.update({name: "nakata"}, {name: "nakata", score: 90}, {upsert: true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name: "nakata"}, {_id: 0})
{ "name" : "nakata", "score" : 90 }
3.4. Delete系操作
3.4.1. 全Documentを削除したい
> db.users.remove({})
WriteResult({ "nRemoved" : 22 })
3.4.1. 検索条件に一致する全Documentを削除したい
> db.users.remove({name: "inaba"})
WriteResult({ "nRemoved" : 10 })
4. Indexに関する操作
4.1. Collectionに定義されているIndexを知りたい
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
}
]
4.2. Indexを作成したい
> db.users.createIndex({score: -1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
},
{
"v" : 1,
"key" : {
"score" : -1
},
"name" : "score_-1",
"ns" : "mydb.users"
}
]
4.3. Indexを削除したい
> db.users.dropIndex("score_-1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
}
]
4.4. ユニークなIndexを作成したい
> db.users.createIndex({name: 1}, {unique: true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.users"
},
{
"v" : 1,
"unique" : true,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "mydb.users"
}
]
> db.users.insert({name: "nakata"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: mydb.users index: name_1 dup key: { : \"nakata\" }"
}
})
5. データベースのバックアップ・復旧を行いたい
5.1. データベースのバックアップをとりたい
$ mongodump -d mydb
2016-01-27T21:57:21.642+0900 writing mydb.users to
2016-01-27T21:57:21.643+0900 done dumping mydb.users (3 documents)
$ ls
total 0
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 .
drwxr-xr-x+ 74 inaba staff 2516 Jan 27 21:57 ..
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 dump
5.2. データベースを復旧したい
$ ls
total 0
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 .
drwxr-xr-x+ 74 inaba staff 2516 Jan 27 21:57 ..
drwxr-xr-x 3 inaba staff 102 Jan 27 21:57 dump
$ mongorestore --drop
2016-01-27T21:58:22.413+0900 using default 'dump' directory
2016-01-27T21:58:22.413+0900 building a list of dbs and collections to restore from dump dir
2016-01-27T21:58:22.414+0900 reading metadata for mydb.users from dump/mydb/users.metadata.json
2016-01-27T21:58:22.414+0900 restoring mydb.users from dump/mydb/users.bson
2016-01-27T21:58:22.480+0900 restoring indexes for collection mydb.users from metadata
2016-01-27T21:58:22.518+0900 finished restoring mydb.users (3 documents)
2016-01-27T21:58:22.518+0900 done