アドベントカレンダーも終わりが見えてきました.
今回私は卒論で利用しているMongoDBについて書こうと思います.
MongoDBとは
MongoDBとは,今流行のNoSQLに分類されるオープンソースのデータベースです.リレーショナルデータベースに近い特徴を持ちながら,スキーマレスにJSONライクなデータを扱うことが出来るものです.
扱う用語に少しRDBMSとは違う部分があるので一応並べておきます.
RDBMS | MongoDB |
---|---|
データベース | データベース |
テーブル | コレクション |
レコード | ドキュメント |
以上のことを踏まえて,MongoDBを触ってみましょう.
やってみよう
では,実際にMongoDBを触ってみましょう.MongoDBにはshellが用意されており,その中ではJavaScriptが実行可能です.
そのshellを使って実際にデータの操作を行っていきます.
まずは基本操作です.
> help //ヘルプの表示
> show dbs //データベースの表示
> use [dbname] //使用するデータベースを[dbname]に切り替える(無ければ作成)
> show collections //現在データベースのコレクションを表示
さぁここからは実際のクエリでデータ操作をしていきましょう.create
まずはデータの挿入です.データの挿入にはinsertを使います.
> db.collection.insert({name: "nashio", age: 21})
> for(var i = 0; i < 10; i++) db.collection.insert({name: "nashio", age: 21, n: i})
これでデータが挿入されます.下のようにJavaScriptでも書けちゃうところが恐ろしいですね.
collectionのところは挿入したいコレクション名を指定します(無ければ作成しれくれます).
挿入するデータはJSONライクなデータ構造で書いていきます.
read
データの検索にはfindを使います.> db.collection.find()
{ "_id" : ObjectId("52b6c2e22a9975309eda256e"), "name" : "nashio", "age" : 21 }
{ "_id" : ObjectId("52b6c4df2a9975309eda256f"), "name" : "nashio", "age" : 21, "n" : 0 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2570"), "name" : "nashio", "age" : 21, "n" : 1 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2571"), "name" : "nashio", "age" : 21, "n" : 2 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2572"), "name" : "nashio", "age" : 21, "n" : 3 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2573"), "name" : "nashio", "age" : 21, "n" : 4 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2574"), "name" : "nashio", "age" : 21, "n" : 5 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2575"), "name" : "nashio", "age" : 21, "n" : 6 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2576"), "name" : "nashio", "age" : 21, "n" : 7 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2577"), "name" : "nashio", "age" : 21, "n" : 8 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2578"), "name" : "nashio", "age" : 21, "n" : 9 }
> db.collection.find({n: 1})
{ "_id" : ObjectId("52b6c4df2a9975309eda2570"), "name" : "nashio", "age" : 21, "n" : 1 }
> db.collection.find({n: {$gt: 3}})
{ "_id" : ObjectId("52b6c4df2a9975309eda2573"), "name" : "nashio", "age" : 21, "n" : 4 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2574"), "name" : "nashio", "age" : 21, "n" : 5 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2575"), "name" : "nashio", "age" : 21, "n" : 6 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2576"), "name" : "nashio", "age" : 21, "n" : 7 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2577"), "name" : "nashio", "age" : 21, "n" : 8 }
{ "_id" : ObjectId("52b6c4df2a9975309eda2578"), "name" : "nashio", "age" : 21, "n" : 9 }
find()で全件表示,中で条件を指定して検索が行われます.検索の条件には{n: 1}でn=1,{n: {$gt: 3}}でn>3で検索される事になります.
$gtの他には$lt(<),$gte(>=),$lte(<=),$ne(!=)などがあります.
update
更新にはupdateを使います.> db.collection.find({n: 1})
{ "_id" : ObjectId("52b6c4df2a9975309eda2570"), "name" : "nashio", "age" : 21, "n" : 1 }
> db.collection.update({n: 1}, {n: 10})
> db.collection.find({n: 1})
> db.collection.find({n: 10})
{ "_id" : ObjectId("52b6c4df2a9975309eda2570"), "n" : 10 }
このような感じで更新が行われます.updateの前の引数は条件,後ろの引数が更新する内容になります.
自然な考え方をするとnが10に変わるだけだと思いますが,MongoDBでは完全にその内容が上書きされます.よって最後のようにドキュメントの内容がn: 10だけになっているのがわかります.
では,本来行いたい変更はどうやるのでしょうか?
それは次のように行います.
> db.collection.find({n: 2})
{ "_id" : ObjectId("52b6caa62a9975309eda2585"), "name" : "nashio", "age" : 21, "n" : 2 }
> db.collection.update({n: 2}, {$set: {n: 20}})
> db.collection.find({n: 2})
> db.collection.find({n: 20})
{ "_id" : ObjectId("52b6caa62a9975309eda2585"), "name" : "nashio", "age" : 21, "n" : 20 }
$setという更新修飾子を使って更新します.このような更新修飾子は他にもたくさんありますが今回は割愛します.
delete
削除にはremoveを使用します.> db.collection.find({n: 20})
{ "_id" : ObjectId("52b6caa62a9975309eda2585"), "name" : "nashio", "age" : 21, "n" : 20 }
> db.collection.remove({n: 20})
> db.collection.find({n: 20})
このような感じで削除が行われます.
引数に削除の条件が指定され,それに該当するドキュメントが削除されます.> db.collection.remove()
これで全件削除することが出来ます.これだけわかれはある程度MongoDBが使えるようになると思います.
もっと詳しいことが知りたい人はMongoDBの薄い本を見ると捗ると思います.
おわりに
みなさんいかがだったでしょうか?結構簡単そうじゃないですか?MongoDBはスキーマレスという特徴もあり,アプリケーションのプロトタイプ作成など気軽に使う分にはすごく簡単で便利だと思います.
本気で使おうと思うと英語の情報でしか見つからなくなってくるというのはありますがすごく便利なデータベースなのでみなさんも試してみてくださいね.
It’s awesome to pay a visit to this web page and reading the views of all friends regarding this paragraph, while I am also eager of getting experience.
返信削除Moviesda
Buffalo Wild Wings wants feedback from its customers so they can provide much better service in the future. BWWListens Survey
返信削除Simple and quick Pasta Salad Recipe. Pasta shells filled with bacon, tomatoes, cucumbers, and cheese, all tossed in a rich freshly made Caesar ... Click here
返信削除The game’s objective is to slide numbered tiles on a 4×4 grid and combine them to create a tile with higher numbers. The game is won when a tile with a value of 2048 appears on the board
返信削除All of us are about you simply being you. Hy-Vee Consumer SurveyThe primary Mill operator's Brew House opened in 1988, in Jupiter, Florida, as a nearby spot to be delighted in by varying backgrounds.
返信削除Thank you for writing such a beautiful and helpful post. The best games for recess can also be found at moto x3m
返信削除