mongoDBの森を歩く

Table of Contents

参考にしたサイト

https://dotinstall.com/ https://ja.wikipedia.org/wiki/MongoDB https://www.mongodb.com/jp http://junaruga.hatenablog.com/entry/2017/11/13/075029

インストール

fedora26の場合

sudo dnf install mongo mongodb-server

起動と自動起動設定

sudo systemctl start mongod
sudo systemctl enable mongod

REPL

mongo

DBの操作

表示

show dbs
use mydb

現在のDBの情報

db.stats();

作成

use <db-name>

削除

db.dropDatabase();

Collectionの操作

作成

db.createCollection("users");

表示

show collections;

リネーム

db.users.renameCollection("costumers");

削除

db.costumers.drop();

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++){ … db.users.insert({ … score: Math.random() … }); … } WriteResult({ “nInserted” : 1 }) db.users.count(); 12 db.users.find(); { “_id” : ObjectId(“5ab85e718a6f3fc0e0564c8d”), “name” : “taguchi”, “score” : 30 } { “_id” : ObjectId(“5ab85e988a6f3fc0e0564c8e”), “name” : “fkoji”, “score” : 50, “tags” : [ “web”, “mobile” ] } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c8f”), “score” : 0.5051645100761789 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c90”), “score” : 0.4473334785545654 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c91”), “score” : 0.9272772678372699 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c92”), “score” : 0.17647580670114926 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c93”), “score” : 0.761121041198397 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c94”), “score” : 0.7212922554688359 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c95”), “score” : 0.03060687445135568 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c96”), “score” : 0.4207822173198339 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c97”), “score” : 0.3180118402761415 } { “_id” : ObjectId(“5ab85eb98a6f3fc0e0564c98”), “score” : 0.9481763669280244 } db.users.remove({}); WriteResult({ “nRemoved” : 12 }) db.users.find();

find条件

db.users.find({team: "team-1"});
db.users.find({score: {$gte: 50}});
db.users.find({name: /t/});
db.users.find({name: /^t/});
db.users.distinct("team");

and文

db.users.find({name: /i/, score:{$gte:50}});

or文

db.users.find({$or: [{name: /i/}, {score:{$gte:50}}}});

in文

db.users.find({score:{$in: [52, 66]}});

exists文

db.users.find({age:{$exists: true}});

表示項目の指定

db.users.find({}, {name: true, score: 1});
db.users.find({}, {score: 0});

※ _idに関しては逆の真偽値を指定できる

sort()

db.users.find({}, {_id:0}).sort({score: 1});
db.users.find({}, {_id: 0}).sort({score: -1});

limit()

db.users.find({}, {_id: 0}).limit(3);
db.users.find({}, {_id: 0}).sort({score: -1}).limit(3);

findOne()

db.users.findOne({}, {_id: 0});

skip()

db.users.findOne({}, {_id: 0}).skip(2);

更新

更新されるのは最初の1件

項目を更新する場合は$setを使う

db.users.update({name: "taguchi"}, {$set: {score: 80}});
db.users.update({name: "taguchi"}, {$set: {score: 80, team: "team-2"}});

まるごと帰る場合はそのまま渡す

db.users.update({name: "taguchi"}, {name: "taguchi", score: 80, team: "team-2"});

複数更新する場合はmultitrueにする

db.users.update({team: "team-1"}, {$set: {score: 0}}, {multi: true});

$inc

加算する

db.users.update({name: "taguchi"}, {$inc: {score: 5}});

$mul

乗算

db.users.update({name: "taguchi"}, {$mul: {score: 5}});

$rename

フィールド名を変更

db.users.update({name: "taguchi"}, {$rename: {score: "point"}});

フィールドを追加

db.users.update({name: "taguchi"}, {$set: {team: "team-2"}});

$unset

db.users.update({name: "taguchi"}, {$unset: {team: ""}});

upsert

データが存在すればupdate、なければinsert

db.users.update({name: "taguchi"}, {name: "taguchi", score: 48}, {upsert: true});

remove()

db.users.remove({name: "kato"});

インデックス

うまく使うと検索時のパフォーマンスが向上する

インデックス情報を取得する

db.users.getIndexes();

scoreに対して降順のインデックスをつける

db.users.createIndex({score: -1});

削除

db.users.dropIndex("score_-1");

ユニークキーにする

db.users.createIndex({name: 1}, {unique: true});

バックアップと復元

バックアップ

mongodump -d mydb

復元

mongorestore --drop