コミュ障だから明日が僕らをよんだって返事もろくにしなかった

何かを創る人に憧れたからブログをはじめたんだと思うよ

きょうのもんご

きょうのもんご

もんごーーいぇー。はい、僕です。皆様は「もんご」とは何かご存知でしょうか?そうですね、仏教用語の一つで、門人の口伝のことを指す言葉ですね。聡明な読者の皆様ならすぐに分かったと思います。僕はつい先日まで、この言葉がなんなのか全然知りませんでした。



さて、という作り話をつくっていたのですが、僕の執筆能力でこれ以上この適当な話を広げるのは無理でした。そういったわけでドットインストールに MongoDBに関する講座があったので終えてみましたという話をすることにします。

公式
MongoDB for GIANT Ideas | MongoDB

ドットインストール
MongoDB入門

件のMongoDBですが、Wiki曰くこんなやつです。

MongoDBはRDBMSではなく、いわゆるNoSQLと呼ばれるデータベースに分類されるものである。RDBMSのようにレコードをテーブルに格納するのではなく、「ドキュメント」と呼ばれる構造的データをJSONライクな形式で表現し、そのドキュメントの集合を「コレクション」として管理する(このデータの物理的な格納はBSONと呼ばれるJSONのバイナリ版といえる形式で行われる)。コレクションはRDBMSのような固定的なスキーマを持たない。ドキュメントには複雑な階層構造を持たせることもでき、それらの構造に含まれるフィールドを指定したクエリやインデクス生成も簡単な指定によって行える。RDBMSのように高度な結合操作を効率的に行うことはできないが、データの追加・更新・削除・クエリは高速に行うことができる。また、アプリケーションは自身の構造やデータ型に合った自然な形でデータを格納することができるため、扱うデータの特性によっては、RDBMSよりも容易かつ迅速に開発を行える可能性がある。

まあ、何言っているのかって感じですけど、NoSQLの一種です。JSONという聞いたこともない拡張子の形式でデータを管理できるやつです。こいつはやばいぜ……。NoSQL系は最近アマゾンのDynamo DBが今熱いとか聞くけど詳細は知らない。


なんかやってみる

てなわけでして、ドットインストール曰く基本操作は以下のようになるそうです。

# 起動
> mongo

# バージョン確認
> mongo --version
MongoDB shell version v4.0.0


データベース操作

> show dbs; #(DBの確認)
> use 【DB名】; #(DBの使用)
> db.state(); #(DBの確認)
> db.dropDatabase(); #(DBの削除)


コレクション操作

> db.createCollection(【コレクション名】);  #(コレクションの作成)
> db.【コレクション名】.renameCollection("【変更後のコレクション名】"); #(コレクション名の変更)
> db.【コレクション名】.drop(); #(コレクションの削除)
> show collection; #(コレクションの確認)


ドキュメントの操作

> db.【コレクション名】.count(); #(ドキュメントの個数の確認)
> db.【コレクション名】.find(); #(全件抽出)
> db.【コレクション名】.insert({【JSON形式データ】}); #(ドキュメントの挿入)
> db.【コレクション名】.update({【JSON形式データ】,【JSON形式データ】}); #(ドキュメントの更新)
> db.【コレクション名】.remove({【JSON形式データ】}); #(ドキュメントの削除)


データベースのバックアップと復元

# バックアップの作成
> mongodump -d 【データベース名】

# バックアップからの復元
> mongorestore 


とりあえず、これだけできれば一通り動かせると思う。更新とか削除とか、細かい検索はクセがあるのと公式やドットインストールのほうが詳しく書いてあるしでいいかって適当に書いてます。


まあ、これから僕がやろうとすることにこれ使わないですけどね(講座内容ガン無視)。何をやるのかと言いますと JavaScript ファイルを実行してデータを流し込むやつをやろうと思います。何を隠そう、この記事は MongoDB ってJavaScriptバッチ処理)使えるやんってのを知って試してみるかってやってみただけの記事だったのです……。しかしながら、JavaScriptでねじ込むネタが思いつかないので "99 Bottles of Beer" を ねじ込もうかと思います。この聞きなれないこいつが何者かと言いますと数え歌です。99からカウントダウンしていく数え歌です。

参考
99 Bottles of Beer | The lyrics to the song 99 Bottles of Beer


バッチ処理的なやつをつくった

MongoDB は以下コマンドでJavaScriptで書いたソースをそのままDBに流し込む事ができるらしいです。

> mongo --quiet 【ホスト名】/【データベース名】  ***.js

なので先ほどの参考歌詞から、適当につくった99-bottlesコードを流し込んでみようと思います。
適当に作った 99-bottlesコード.js

let bottles = {};
for (var i = 99; i>= 0 ; i--) {    
  if(i > 1){
    bottles = {
        text1 :  i + " bottles of beer on the wall," + i +"bottles of beer.",
        text2 : "Take one down and pass it around, "+(i-1)+" bottles of beer on the wall."
    };
  }else if(i == 1){
    bottles = {
        text1 :  i + " bottle of beer on the wall," + i +"bottle of beer.",
        text2 : "Take one down and pass it around, no more bottles of beer on the wall."
    };     
  }else if(i == 0){
    bottles = {
        text1 : "No more bottles of beer on the wall, no more bottles of beer. ",
        text2 : "Go to the store and buy some more, 99 bottles of beer on the wall."
    };
  }
  db.bottles.insert(bottles);
}

またクソコードを生み出してしまった。



作ったので確認してみます。

> mongo --quiet localhost/bottles 99bottles.js

実行結果

> db.bottles.count();
100
> db.bottles.find({},{_id:0});
{ "text1" : "99 bottles of beer on the wall,99bottles of beer.", "text2" : "Take one down and pass it around, 98 bottles of beer on the wall." }
{ "text1" : "98 bottles of beer on the wall,98bottles of beer.", "text2" : "Take one down and pass it around, 97 bottles of beer on the wall." }
{ "text1" : "97 bottles of beer on the wall,97bottles of beer.", "text2" : "Take one down and pass it around, 96 bottles of beer on the wall." }
{ "text1" : "96 bottles of beer on the wall,96bottles of beer.", "text2" : "Take one down and pass it around, 95 bottles of beer on the wall." }
{ "text1" : "95 bottles of beer on the wall,95bottles of beer.", "text2" : "Take one down and pass it around, 94 bottles of beer on the wall." }
~ 略 ~
{ "text1" : "5 bottles of beer on the wall,5bottles of beer.", "text2" : "Take one down and pass it around, 4 bottles of beer on the wall." }
{ "text1" : "4 bottles of beer on the wall,4bottles of beer.", "text2" : "Take one down and pass it around, 3 bottles of beer on the wall." }
{ "text1" : "3 bottles of beer on the wall,3bottles of beer.", "text2" : "Take one down and pass it around, 2 bottles of beer on the wall." }
{ "text1" : "2 bottles of beer on the wall,2bottles of beer.", "text2" : "Take one down and pass it around, 1 bottles of beer on the wall." }
{ "text1" : "1 bottle of beer on the wall,1bottle of beer.", "text2" : "Take one down and pass it around, no more bottles of beer on the wall." }
{ "text1" : "No more bottles of beer on the wall, no more bottles of beer. ", "text2" : "Go to the store and buy some more, 99 bottles of beer on the wall." }

出力結果よく見てないけど、おそらく大丈夫でしょう。MongoDBだと、こんな感じにJavaScriptでデータいじれるんですね。へー。



おしまい