LoginSignup
15
15

More than 5 years have passed since last update.

mongoDBのコマンドメモ

Last updated at Posted at 2017-02-08

自分用にまとめています

概要

  • ドキュメント指向
  • スキーマレス(データ構造を決めない)
  • Databeseの中にCollectionをいくつも作れる
  • Collectionの中にDocumentをいくつも作れる

Collection(Posts Usersなど)

→テーブル

Document

→レコード

  • field: val;
  • field: value;
  • スキーマレス

コマンド一覧

database一覧

show hoge

databese作成

use hoge

Collection作成

db.createCollection("hoge");

database情報

db.stats();

database削除

db.dropDatabase();

Collection一覧

show collections;

Collection名を変更 users->customers

db.users.renameCollection("customers");

Collection削除

db.hoge.drop();

Document作成

db.hoge.insert(
    {
        name: "Taro Yamada",
        score: 30
    }
)

スキーマレス(tagsを追加)

db.hoge.insert({
    name: "Taro Yamada",
    score: 50,
    tags: ["web", "mobile"]
});

jsを使ってinsertができる

for (var i = 0; i < 10; i++ ) {
    db.hoge.insert({
        score: Math.random();
    });
}

Documentの数

db.hoge.count();

Document全データを表示

db.hoge.find();

Document全件削除

db.hoge.remove({});

teamがteam1のみ抽出

db.hoge.find({ team: "team-1" });

scoreが50以上を抽出

db.hoge.find({ score: { $gte: 50 } });

// $gte $gt $lte $lt 以上 未満
// $eq イコール
// $ne イコールでない

teamがteam1のみを抽出

db.hoge.find({ team: { $eq: "team-1" } });

nameに「t」が含まれるのを抽出(正規表現)

db.hoge.find({ name: /t/ });

teamの値を抽出

db.hoge.distinct("team");

nameに「i」が含まれていて、かつスコアが50点以上

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

nameに「i」が含まれている、またはスコアが50点以上

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

scoreが52点または66点のものを抽出

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

ageが存在するDocumentを抽出

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

全データで表示するフィールドを指定(第2引数)

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

scoreを含まない全データを表示

db.hoge.find({}, { score: 0 });

idはデフォルトで表示されるのでidを非表示でscoreのみを表示。id以外は1と0、trueとfalseの乱用は不可

db.hoge.find({}, { score: 1, _id: 0 });

socreを小さい順に表示

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

3件のみ表示

db.hoge.find({}, { _id: 0 }).limit(3);

スコアが上位3名のものを表示

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

最初の1件のみを表示

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

全データを3件目から表示

db.hoge.find({}, { _id: 0 }).skip(2);

yamadaさんのscoreのドキュメントを80に更新

db.hoge.update({ name: "yamada" }, { $set: { score: 80 } });

yamadaさんのscoreとteamのドキュメントを更新

db.hoge.update({ name: "yamada" }, { $set: { score: 90, team: "team-2" } });

setではない場合、すべてのドキュメントが変更、削除される

db.hoge.update({ name: "yamada" }, { name: "sato" score: 90 });

第3引数にmulti:trueにすると合致したドキュメント(team2)の全てのデータのscoreが0に変更される

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

特定のドキュメントをincで値を追加できる

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

特定のドキュメントをmulで掛け算できる

db.hoge.update({ name: "yamada" }, { $mul: { score: 2 } });

フィールドの名前を変更したい場合 socre->point

db.hoge.update({ name: "yamada" }, { $rename: { soore:  "point" } });

フィールドを追加と値を入れる

db.hoge.update({ name: "yamada" }, { $set: { team: "team-4" } });

フィールドを削除(値も削除)

db.hoge.update({ name: "yamada" }, { $unset: { team: "" } });

存在しないフィールドに対して更新させたい場合は第3引数にupsertを書く(scoreが存在しなかった)

db.hoge.update({ name: "yamada" }, { name: "sato", score: 48 }, { upsert: true });

特定のドキュメントを削除

db.hoge.remove({ name: "taro" });

index情報表示

db.hoge.getIndexes();

降順でindexを追加

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

indexを削除

db.hoge.dropIndex("score_1");

exitでMongoDBを抜ける

exit

バックアップ

mongodump -d mydb

データ復元

mongorestore --drop
15
15
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
15
15