LoginSignup
5
5

More than 5 years have passed since last update.

ubuntu 13.10 MongoDBの導入

Last updated at Posted at 2014-03-22

山ほど似たようなものありそうだけどまぁ、メモ
適当なインストールと、ちょっとだけMongoDBを触ってみる

作業環境

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=13.10
DISTRIB_CODENAME=saucy
DISTRIB_DESCRIPTION="Ubuntu 13.10"

参考サイト

インストール部分

MongoDBの公式解説
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

使ってみる部分

MongoDBの薄い本
http://www.cuspy.org/diary/2012-04-17/the-little-mongodb-book-ja.pdf

手順

基本的には上記参考サイト同じ事してるだけ

aptのパッケージに矛盾がないことを保証するため MongoDB public GPG Key をインポート

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

/etc/apt/sources.list.d/にmongodbのリポジトリ設定を追加

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

いつもの

sudo apt-get update
sudo apt-get install mongodb-10gen

バージョンが指定したいならこうすればいいらしい(未確認)

apt-get install mongodb-10gen=2.2.3

使ってみる

じゃあ早速、楽しませてもらおっかな〜!

起動

$ sudo service mongodb start
start: Job is already running: mongodb

MongoShellを動かす

$ mongo
MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
> 

コマンド一覧を見てみる

db.help()

コマンドがどうなってんのか見てみる
ちょっと面白い

db.help

現在のdb状態がどんなものか見るために、db.status()を使う

> db.stats()
{
    "db" : "test",
    "collections" : 0,
    "objects" : 0,
    "avgObjSize" : 0,
    "dataSize" : 0,
    "storageSize" : 0,
    "numExtents" : 0,
    "indexes" : 0,
    "indexSize" : 0,
    "fileSize" : 0,
    "nsSizeMB" : 0,
    "dataFileVersion" : {

    },
    "ok" : 1
}

コレクションを作ってみる

> db.games.insert({name: 'Garden', developer: 'Cuffs' })

ということで gamesコレクションに、名前と制作会社をぶち込んでみた

> db.stats()
{
    "db" : "test",
    "collections" : 3,
    "objects" : 5,
    "avgObjSize" : 46.4,
    "dataSize" : 232,
    "storageSize" : 12288,
    "numExtents" : 3,
    "indexes" : 1,
    "indexSize" : 8176,
    "fileSize" : 201326592,
    "nsSizeMB" : 16,
    "dataFileVersion" : {
        "major" : 4,
        "minor" : 5
    },
    "ok" : 1
}

collectionsが 3
objectsが 5
他にもいろいろと変わった

とりあえずCollectionを確認してみよう

> db.getCollectionNames()
[ "games", "system.indexes" ]

collectionsのあと1つはどこへ行ったの…(小声)

追加でさっき追加したデータも見てみよう

> db.games.find()
{ "_id" : ObjectId("532d2176c0268538d80080c1"), "name" : "Garden", "developer" : "Cuffs" }

こっちには一意っぽい _id が追加されている
さらにこの _id はインデックス化されているようです

> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.games", "name" : "_id_" }

せっかくなんでもう一件データを追加してみよう。
graphickerの項目を増やしてみる

>db.games.insert({name: 'YourDialy', developer : 'CUBE', graphicker : 'Kantoku' })

> db.games.find()
{ "_id" : ObjectId("532d2176c0268538d80080c1"), "name" : "Garden", "developer" : "Cuffs" }
{ "_id" : ObjectId("532d280c90c679088ba0a3f9"), "name" : "YourDialy", "developer" : "CUBE", "graphicker" : "Kantoku" }

graphicker が新たに追加されてる事がわかる
でもGardenの方には graphicker がないままなんですねぇ…


とまぁ…簡単に触ってみるのはこのくらいで一旦終わりにしましょ
あとは薄い本見ながら触ってみるのが良さそうです

以上!

5
5
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
5
5