MongoDBとは
RDBMS | データの構造を決めてからデータを管理(MySQLなど) |
NoSQL | スキーマレス(mongoDBなど) |
MongoDBのインストール
$ sudo vi /etc/yum.repos.d/mongodb-org-4.0.repo # リポジトリの作成 [mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc $ sudo yum install -y mongodb-org # mongoDBのインストール $ sudo service mongod start # mongoDBの起動 $ sudo chkconfig mongdd on # 再起動時に自動起動されるように設定 $ mongo --version # バージョン確認 MongoDB shell version v4.0.5
用語の理解
MySQL | MongoDB |
---|---|
データベース | データベース |
テーブル | コレクション(Collection) |
レコード | ドキュメント(Document) |
カラム | フィールド(field) |
Documentは、{field: val, field: val, …}のようにJavaScriptのオブジェクト形式で指定します。
※ MongoDBをPythonで利用する方法は下記が参考になりました。
pymongoの基本的な使い方まとめ
データベースの操作
$ mongo -- mongoDBにアクセス > 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 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; -- DB一覧を表示 local 0.000GB > use mydb; -- DB(mydb)を作成して切り替える switched to db mydb > db.createCollection("users"); -- usersコレクションを作成 { "ok" : 1 } > show dbs; local 0.000GB mydb 0.000GB -- コレクションを作成して初めて認識される > db.stats(); -- 現在のDB(mydb)の情報を表示 { "db" : "mydb", "collections" : 1, "views" : 0, "objects" : 0, "avgObjSize" : 0, "dataSize" : 0, "storageSize" : 4096, "numExtents" : 0, "indexes" : 1, "indexSize" : 4096, "fsUsedSize" : 23119659008, "fsTotalSize" : 53660876800, "ok" : 1 } > db.dropDatabase(); -- データベースの削除 { "dropped" : "mydb", "ok" : 1 } > show dbs; -- mydbが削除されていることを確認 local 0.000GB > exit -- 抜ける bye
コレクションの操作
$ mongo > use mydb; switched to db mydb > db.createCollection("users"); { "ok" : 1 } > show collections; users > db.users.renameCollection("customers"); -- コレクション名を変更 { "ok" : 1 } > show collections; -- コレクション名が変更されていることを確認 customers > db.customers.drop(); -- コレクションを削除 true > show collections; -- コレクションが削除されていることを確認 > > db.dropDatabase(); { "dropped" : "mydb", "ok" : 1 }
ドキュメントの操作
> use mydb; switched to db mydb > db.users.insert( -- ドキュメントを挿入するとコレクションが自動的に作成される ... { ... name: "taguchi", ... score: 30 ... } ... ); WriteResult({ "nInserted" : 1 }) > show collections; users > db.users.insert({ ... name: "fkoji", ... score: 50, ... tags: ["web", "mobile"] -- スキーマレスなので、全然違う構造のデータも入れることができます ... }); WriteResult({ "nInserted" : 1 }) > for (var i = 0; i < 10; i++) { -- MongoDBはJavaScriptが使えるので、ループ文を使って一気にデータを挿入することもできます ... db.users.insert({ ... score: Math.random() ... }); ... } WriteResult({ "nInserted" : 1 }) > db.users.count(); -- ドキュメントの個数を確認 12 > db.users.find(); -- 全データを表示 { "_id" : ObjectId("5d13072c8fc4cf386a776da3"), "name" : "taguchi", "score" : 30 } { "_id" : ObjectId("5d1307e88fc4cf386a776da4"), "name" : "fkoji", "score" : 50, "tags" : [ "web", "mobile" ] } { "_id" : ObjectId("5d1308a48fc4cf386a776da5"), "score" : 0.52152266226252 } { "_id" : ObjectId("5d1308a48fc4cf386a776da6"), "score" : 0.26043558393205 } { "_id" : ObjectId("5d1308a48fc4cf386a776da7"), "score" : 0.9369121989317596 } { "_id" : ObjectId("5d1308a48fc4cf386a776da8"), "score" : 0.6399389093384168 } { "_id" : ObjectId("5d1308a48fc4cf386a776da9"), "score" : 0.7364044752736815 } { "_id" : ObjectId("5d1308a48fc4cf386a776daa"), "score" : 0.44497153453138905 } { "_id" : ObjectId("5d1308a48fc4cf386a776dab"), "score" : 0.17180108837922825 } { "_id" : ObjectId("5d1308a48fc4cf386a776dac"), "score" : 0.7919468981709256 } { "_id" : ObjectId("5d1308a48fc4cf386a776dad"), "score" : 0.6013791869230255 } { "_id" : ObjectId("5d1308a48fc4cf386a776dae"), "score" : 0.7151844721208501 } > db.users.remove({}); -- 空のオブジェクトを渡すことで全件削除 WriteResult({ "nRemoved" : 12 }) > db.users.find(); -- 全件削除を確認 >
条件付きでドキュメントを抽出
-- テストデータ作成 > db.users.insert({ name: "taguchi", score: 52, team: "team-1" }); WriteResult({ "nInserted" : 1 }) > db.users.insert({ name: "fkoji", score: 82, team: "team-2" }); WriteResult({ "nInserted" : 1 }) > db.users.insert({ name: "dotinstall", score: 66, team: "team-3" }); WriteResult({ "nInserted" : 1 }) > db.users.insert({ name: "yamada", score: 26, team: "team-1" }); WriteResult({ "nInserted" : 1 }) > db.users.insert({ name: "kimura", score: 29, team: "team-2" }); WriteResult({ "nInserted" : 1 }) > db.users.find(); -- 確認 { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 82, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 29, "team" : "team-2" } > db.users.find({team: "team-1"}); -- teamがteam-1のものを抽出 { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } > db.users.find({score: {$gte: 50}}); -- scoreが50点以上のものを抽出($gt,$gte,$lt,$lte,$eq,$neが使用可能) { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 82, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } > db.users.find({name: /t/}); -- 名前に"t"を含むものを抽出 { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } > db.users.find({name: /^t/}); -- JavaScriptの正規表現も使用可能 { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } > db.users.distinct("team"); -- フィールドの値を確認 [ "team-1", "team-2", "team-3" ]
複雑な条件でドキュメントを抽出
> db.users.find({name:/i/, score:{$gte:50}}); -- nameに"i"が含まれていてかつscoreが50点以上 { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 82, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } > db.users.find({$or: [{name:/i/}, {score:{$gte:50}}]}); -- or条件の場合 { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 82, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 29, "team" : "team-2" } > db.users.find({score: {$in: [52, 66]}}); -- 同じフィールド内であれば左記のように記述できる { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52, "team" : "team-1" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } > db.users.insert({name:"tanaka", score:52, age:23}); WriteResult({ "nInserted" : 1 }) > db.users.find({age: {$exists: true}}); -- ageというフィールドがあるものだけ抽出 { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52, "age" : 23 }
表示するフィールドを指定
-- 全件抽出の場合の第一引数は空のオブジェクト、第二引数で表示するフィールドを指定("true"または"1"を指定) > db.users.find({}, {name: true, score: 1}); { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 52 } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 82 } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66 } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26 } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 29 } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52 } > db.users.find({}, {score: 0}); -- scoreを非表示にする("false"または"0"を指定) { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "team" : "team-1" } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "team" : "team-2" } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "age" : 23 } > db.users.find({}, {score: 1, _id: 0}); -- scoreを表示して、_idは非表示にする(_idに関しては、"0"と"1"が混在可能) { "score" : 52 } { "score" : 82 } { "score" : 66 } { "score" : 26 } { "score" : 29 } { "score" : 52 }
sort()、limit()、skip()を使う
> db.users.find({}, {_id: 0}).sort({score: 1}); -- scoreを昇順に表示(sort) { "name" : "yamada", "score" : 26, "team" : "team-1" } { "name" : "kimura", "score" : 29, "team" : "team-2" } { "name" : "taguchi", "score" : 52, "team" : "team-1" } { "name" : "tanaka", "score" : 52, "age" : 23 } { "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "name" : "fkoji", "score" : 82, "team" : "team-2" } > db.users.find({}, {_id: 0}).sort({score: -1}); -- scoreを降順に表示 { "name" : "fkoji", "score" : 82, "team" : "team-2" } { "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "name" : "taguchi", "score" : 52, "team" : "team-1" } { "name" : "tanaka", "score" : 52, "age" : 23 } { "name" : "kimura", "score" : 29, "team" : "team-2" } { "name" : "yamada", "score" : 26, "team" : "team-1" } > db.users.find({}, {_id: 0}).sort({score: -1}).limit(3); -- 上位3件だけ表示(limit) { "name" : "fkoji", "score" : 82, "team" : "team-2" } { "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "name" : "taguchi", "score" : 52, "team" : "team-1" } > db.users.findOne({}, {_id: 0}); -- 1件だけ表示したい場合は「findOne」を使用 { "name" : "taguchi", "score" : 52, "team" : "team-1" } > db.users.find({}, {_id: 0}).skip(2); -- 最初の2件をスキップして3件目から表示(skip) { "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "name" : "yamada", "score" : 26, "team" : "team-1" } { "name" : "kimura", "score" : 29, "team" : "team-2" } { "name" : "tanaka", "score" : 52, "age" : 23 }
ドキュメントを更新する
-- scoreを80点に更新 > 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, "team" : "team-1" } -- 複数のフィールドを更新 > db.users.update({name: "taguchi"}, {$set: {score: 90, team: "team-2"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({name:"taguchi"}, {_id:0}); { "name" : "taguchi", "score" : 90, "team" : "team-2" } -- 全フィールドを更新 > db.users.update({name: "taguchi"}, {name: "taguchi", score: 40}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({name:"taguchi"}, {_id:0}); { "name" : "taguchi", "score" : 40 } -- 第三引数に"multi"というオプションを与えると、抽出条件に合致する全てのドキュメントを更新できます > db.users.update({team:"team-2"}, {$set: {score: 0}}, {multi: true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.users.find(); { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "score" : 40 } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52, "age" : 23 }
$inc、$mul、$rename、$unsetを使う
-- 5点プラスする($inc) > db.users.update({name: "taguchi"}, {$inc: {score: 5}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({name:"taguchi"}, {_id:0}); { "name" : "taguchi", "score" : 45 } -- scoreを2倍する($mul) > db.users.update({name: "taguchi"}, {$mul: {score: 2}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({name:"taguchi"}, {_id:0}); { "name" : "taguchi", "score" : 90 } -- フィールドの名前を変更 > db.users.update({name: "taguchi"}, {$rename: {score: "point"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({name:"taguchi"}, {_id:0}); { "name" : "taguchi", "point" : 90 } -- フィールドの追加($set) > db.users.update({name: "taguchi"}, {$set: {team: "team-4"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({name:"taguchi"}, {_id:0}); { "name" : "taguchi", "point" : 90, "team" : "team-4" } -- フィールドの削除($unset) > db.users.update({name: "taguchi"}, {$unset: {team: ""}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({name:"taguchi"}, {_id:0}); { "name" : "taguchi", "point" : 90 }
upsert、removeを使う
-- 抽出条件に合致しなかった場合に新規にドキュメントを作成(upsert) > db.users.update({name: "kato"}, {name: "kato", score: 48}, {upsert: true}); WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("5d1326a1a2fa2054d5c46a29") }) > db.users.find(); { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "point" : 90 } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52, "age" : 23 } { "_id" : ObjectId("5d1326a1a2fa2054d5c46a29"), "name" : "kato", "score" : 48 } -- 既にドキュメントが存在する場合は更新(upsert) > db.users.update({name: "kato"}, {name: "kato", score: 99}, {upsert: true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find(); { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "point" : 90 } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52, "age" : 23 } { "_id" : ObjectId("5d1326a1a2fa2054d5c46a29"), "name" : "kato", "score" : 99 } -- 特定のドキュメントを削除(remove) > db.users.remove({name: "kato"}); WriteResult({ "nRemoved" : 1 }) > db.users.find(); { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "point" : 90 } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52, "age" : 23 }
索引を使う
-- indexの設定を表示 > db.users.getIndexes(); [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.users" } ] -- scoreに対して降順のindexを作成(createIndex) > db.users.createIndex({score: -1}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.users.getIndexes(); [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.users" }, { "v" : 2, "key" : { "score" : -1 # scoreに対して降順のindexが作成 }, "name" : "score_-1", # index名は「score_-1」 "ns" : "mydb.users" } ] -- indexの削除(dropIndex) > db.users.dropIndex("score_-1"); { "nIndexesWas" : 2, "ok" : 1 } > db.users.getIndexes(); [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.users" } ] -- uniqueキーを使用したindexの作成 > db.users.createIndex({name: 1}, {unique: true}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.users.getIndexes(); [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.users" }, { "v" : 2, "unique" : true, "key" : { "name" : 1 }, "name" : "name_1", "ns" : "mydb.users" } ] > db.users.insert({name: "taguchi"}); # 既にある名前はinsertできない WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: mydb.users index: name_1 dup key: { : \"taguchi\" }" } })
mongodump、mongorestoreを使う
$ mongodump -d mydb 2019-06-26T17:30:18.530+0900 writing mydb.users to 2019-06-26T17:30:18.532+0900 done dumping mydb.users (6 documents) $ ls -F dump/ $ mongo mydb # DB名を指定してアクセス > db.users.remove({name: "taguchi"}); # taguchiユーザを削除 WriteResult({ "nRemoved" : 1 }) > db.users.find(); { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52, "age" : 23 } > exit # 同じデータベースが存在する場合は、--dropオプションで上書きできます $ mongorestore --drop 2019-06-26T17:39:22.717+0900 using default 'dump' directory 2019-06-26T17:39:22.717+0900 preparing collections to restore from 2019-06-26T17:39:22.744+0900 reading metadata for mydb.users from dump/mydb/users.metadata.json 2019-06-26T17:39:22.853+0900 restoring mydb.users from dump/mydb/users.bson 2019-06-26T17:39:22.855+0900 restoring indexes for collection mydb.users from metadata 2019-06-26T17:39:22.917+0900 finished restoring mydb.users (6 documents) 2019-06-26T17:39:22.917+0900 done $ mongo mydb > db.users.find(); # taguchiユーザが復元されていることを確認 { "_id" : ObjectId("5d130df68fc4cf386a776daf"), "name" : "taguchi", "point" : 90 } { "_id" : ObjectId("5d130e698fc4cf386a776db0"), "name" : "fkoji", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d130e898fc4cf386a776db1"), "name" : "dotinstall", "score" : 66, "team" : "team-3" } { "_id" : ObjectId("5d130e9c8fc4cf386a776db2"), "name" : "yamada", "score" : 26, "team" : "team-1" } { "_id" : ObjectId("5d130eae8fc4cf386a776db3"), "name" : "kimura", "score" : 0, "team" : "team-2" } { "_id" : ObjectId("5d1319898fc4cf386a776db4"), "name" : "tanaka", "score" : 52, "age" : 23 } # 下記コマンドで、色々なオプションが確認できます(コレクション単位でのバックアップや、バックアップファイルの場所を変更等) $ mongodump --help $ mongorestore --help
[siteorigin_widget class=”AdWidgetItem”][/siteorigin_widget]
[siteorigin_widget class=”WP_Widget_Search”][/siteorigin_widget]
[siteorigin_widget class=”WP_Widget_Pages”][/siteorigin_widget]
[siteorigin_widget class=”AdWidgetItem”][/siteorigin_widget]