åå¼·ä¼ç¨ã®ååºãå±ãã¦ã¦ãã¦ãï¼ï¼ï¼ãª @madapaja ã§ãã
é¢é£è¨äºãã©ãã
- 2.0 ããã¯ããã MongoDB
- ãçªå¤ç·¨ãMongoDB ã®æ´å²ï¼MongoDB 2.0 ã¾ã§ï¼
ãã®é£è¼ã§ã¯ MongoDB ãããããã¯ãããæ¹ã«åãã¦ãMongoDB 2.0 ã§è¿½å ãããæ©è½ãå«ãã ãMongoDB å ¨ä½ãåºããç´¹ä»ãã¦ããããã¨æã£ã¦ãã¾ãã
ä»åã®ã¢ã¸ã§ã³ã
- ãªã MongoDB ã
- MongoDB ã®ã¤ã³ã¹ãã¼ã«
- MongoDB ã·ã§ã«ã¨æ¯ãã
ãªã MongoDB ã
MongoDB ã¯ãã1ï½2å¹´ã§æ³¨ç®ãé«ã¾ã£ããNOSQLï¼Not Only SQLï¼ã¨å¼ã°ãããâéâRDBMSï¼Relational DataBase Management Systemï¼ãªãã¼ã¿ãã¼ã¹ã®ä¸ã¤ã§ãã
æ§ã
ãªæã§è°è«ããã¦ããããã«ãNOSQL ãã¼ã¿ãã¼ã¹ã¯æ¢åã® RDBMS ã«å®å
¨ã«ç½®ãæããåå¨ã§ã¯ãªããããããã®é·æã»çæãç解ããä¸ã§ãRDBMS 㨠NOSQL ããã¾ãçµã¿åããããã¨ã§ãããæè»ã§ã¹ã±ã¼ã«ããããã·ã¹ãã ãä½ããã¨ãå¯è½ã§ãããã
ãã¦ããã® NOSQL ã®ä¸ã§ã MongoDB ã注ç®ããã¦ããçç±ã¯ããã®ãç°¡åããã«ããã§ããããMongoDB ã«ã¯ã¤ã³ã¹ãã¼ã«ãéç¨ãã¯ã¨ãªï¼åãåããï¼ãã¤ã³ããã¯ã¹ãªã©æ§ã
ãªç¹ã§ã¨ã³ã¸ãã¢ãæã£ããã¨ãå®ç¾ããæ©è½ãå
å®ãã¦ãã¾ããç¹ã«ã¯ã¨ãªãã¤ã³ããã¯ã¹ã®æ©è½ã«ã¤ãã¦ã¯ãæè»ã§è±å¯ãªã¯ã¨ãªã¨ãã©ã®å±æ§ã«å¯¾ãã¦ãã¤ã³ããã¯ã¹ãä½æã§ãããã«ã»ã¤ã³ããã¯ã¹ã®ãµãã¼ãã«ãããæ¢åã® RDBMS ããã®ç§»è¡ã§ãæ¸æããã¨ãªãéçºã«å°å¿µããã¦ããã¾ãã
ããå°ãå¾æ¼ãããããã«ãæ¡ç¨å®ç¸¾ã«ã触ãã¦ãããã¨æãã¾ãã
æ¡ç¨å®ç¸¾ã¯ãå
¬å¼ãµã¤ãã® Production Deployments ã«æ²è¼ããã¦ãã¾ãã®ã§ããã®ä¸ããããã¤ãããã¯ã¢ãããã¦ãç´¹ä»ãã¾ãã
- Disney
- ã¤ã³ã¿ã©ã¯ãã£ãã¡ãã£ã¢ã°ã«ã¼ãå ã®ãã¹ã¦ã®ã²ã¼ã ã®å ±éã®ãã¼ã«ã¨ API ã®ãã¹ãã¼ã¿ã¹æ å ±ãä¿æããã³ã¢ã³ã»ãªãã¸ã§ã¯ãã»ãªãã¸ããªã¨ã㦠MongoDB ã使ããã¦ãã¾ãã
- foursquare
- ä½ç½®ãã¼ã¹ã®ã½ã¼ã·ã£ã«ãããã¯ã¼ã¯ã§ãã foursquare ã¯ãå ´æã¨å ´æã¸ã®ã¦ã¼ã¶ã¼ããã§ãã¯ã¤ã³ããä¿æããããã«ãMongoDB ã使ããã¦ãã¾ãã
- bit.ly
- ãªã³ã¯ã®ç縮ãã·ã§ã¢ã¨ãã©ããã³ã°ãè¡ãã bit.ly ã§ã¯ãMongoDB ãã¦ã¼ã¶ã¼å±¥æ´ãä¿æããããã«ä½¿ããã¦ãã¾ãã
- SourceForge
- MongoDB ã SourceForge ã®å ¨ããã¸ã§ã¯ãã®ããã³ããã¼ã¸ãããã¸ã§ã¯ããã¼ã¸ããã¦ã³ãã¼ããã¼ã¸ã®ããã¯ã¨ã³ãã¹ãã¬ã¼ã¸ã¨ãã¦ä½¿ããã¦ãã¾ãã
- github
- ã½ã¼ã·ã£ã«ã³ã¼ãã£ã³ã°ãµã¤ãã® GitHub ã§ã¯ãå é¨ã®ãªãã¼ãã£ã³ã°ã¢ããªã±ã¼ã·ã§ã³ã§ MongoDB ã使ããã¦ãã¾ãã
- æ ªå¼ä¼ç¤¾ããªãã¡ã¼ãã¤ã³ãã©ã¹ãã©ã¯ãã£ã¼
- æ ªå¼ä¼ç¤¾ããªãã¡ã¼ãã¤ã³ãã©ã¹ãã©ã¯ãã£ã¼ã®ãæ§ã ãªæ¤ç´¢ãã¼ãºã«å¯¾å¿ããâçµ±åæ¤ç´¢ãã©ãããã©ã¼ã âSedueï¼ã»ãã¥ã¼ï¼ã§ã¯ãç¾å¨ãMongoDB ã使ç¨ãã¦æè»æ§ãå¯ç¨æ§ã®é«ãæ¹æ³ã§ããã¥ã¡ã³ããä¿åããã¦ãã¾ãã
- CyberAgent, Inc.
- CyberAgent, Inc. ã§ã¯ãAmeba Picoï¼ãã°ã®æµ·å¤çï¼ãAmeba tappieï¼ã¿ããã¼ï¼ãµã¼ãã¹ã«ã¦ MongoDB ã使ããã¦ãã¾ãã
ã§ã¯ãããããå®éã« MongoDB ã触ãã¦ããã®ä¸çãä½é¨ãã¦ããã ãããã¨æãã¾ãã
MongoDB ãæè»ã§ããã¯ã¼ããããã¹ãã¼ããåºã¦ãç°¡åã«ä½¿ããããã«è¨è¨ããã¦ãããã¨ãããã¨ã第0åããç´¹ä»ãã¦ããããã§ããããã®ææ³ã¯è³ãæã«ç¾ãã¦ãã¾ãã
ãã¡ããã¤ã³ã¹ãã¼ã«ãã³ã³ã½ã¼ã«ãä¾å¤ã§ã¯ããã¾ãããããã«ç°¡åã§ãã¯ãã«ãã触ããªããå®æãã¦é ããã°ã¨æãã¾ãã
MongoDB ã®ã¤ã³ã¹ãã¼ã«
MongoDB ã¯æ§ã
ãªãã©ãããã©ã¼ã ã¸ç°¡åã«ã¤ã³ã¹ãã¼ã«ã§ãã¾ããMongoDB ãã¦ã³ãã¼ããã¼ã¸ ã«ã¯ãMac OS XãLinuxãWindowsãSolarisç¨ã®ãã¤ããªãæä¾ããã¦ãã¾ããã¾ããå¿
è¦ã§ããã°ã½ã¼ã¹ã³ã¼ããèªåã§ã³ã³ãã¤ã«ãããã¨ãã§ãã¾ãã
ããã§ã¯ãããã¤ãã® OS ã¸ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ãç´¹ä»ãã¦ããã¾ãã
ãªããåOSå
±ã« 32bit ç㨠64bit çã®2種é¡ãç¨æããã¦ãã¾ããã64bit OS ã§ããã° 64bit çã®ã¤ã³ã¹ãã¼ã«ãæ¨å¥¨ãã¾ãã32bit çã® MongoDB ããã»ã¹ã¯ãç´ 2.5GB ã®ãã¼ã¿ã«å¶éããã¾ãã
Mac OS X ã¸ã®ã¤ã³ã¹ãã¼ã«
Mac OS X ã¸ã®ã¤ã³ã¹ãã¼ã«ã«ã¯ããã±ã¼ã¸ããã¼ã¸ã£ã使ãã®ã楽ã§ããããã§ã¯ãHomebrew ã§ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ããç´¹ä»ãã¾ãã
Homebrew 㧠MongoDB ãã¤ã³ã¹ãã¼ã«ããã«ã¯ãã¿ã¼ããã«ã§ä¸è¨ã®ã³ãã³ããå®è¡ãã¾ãã
$ brew update
$ brew install mongodb
ã¤ã³ã¹ãã¼ã«æã«è¡¨ç¤ºãããã¡ãã»ã¼ã¸ã«å¾ã£ã¦ãã¤ã³ã¹ãã¼ã«ãå®äºããã¾ãã
åãã¦ã®ã¤ã³ã¹ãã¼ã«ã§ããã°ã¤ã³æã«èªåèµ·åãããå ´åã¯ã以ä¸ã®ãããªæãã«ãªãã¾ãã
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/mongodb/2.0.0-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist
Linux ã¸ã®ã¤ã³ã¹ãã¼ã«
ããã§ã¯ãããã±ã¼ã¸ã使ã£ãã¤ã³ã¹ãã¼ã«æ¹æ³ããç´¹ä»ãã¾ãã
CentOS / Fedora ã¸ã®ã¤ã³ã¹ãã¼ã«
yum ãªãã¸ããªãæä¾ããã¦ããã®ã§ãã使ãã®ã·ã¹ãã ã«åãã㦠/etc/yum.repos.d/10gen.repo
ã«ä¸è¨ãæ¸ãã¦ããªãã¸ããªãç»é²ãã¾ãã
64bit ç
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
32bit ç
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0
ãã®å¾ãyum ã§ã¤ã³ã¹ãã¼ã«ã MongoDB ãµã¼ãã¼ãèµ·åãã¾ãããã
$ sudo yum install mongo-10gen mongo-10gen-server
$ sudo /etc/init.d/mongod start
ãªããã·ã¹ãã èµ·åæã«éå§ããããã«ãããå ´åã¯ãchkconfig ã³ãã³ãã§ç»é²ãã¾ãã
$ sudo chkconfig --add mongod
Ubuntu ã¸ã®ã¤ã³ã¹ãã¼ã«
apt ãªãã¸ããªãæä¾ããã¦ãã¾ãã
ããã§ã¯ãæè¿ã® Ubuntu ã¸ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ãç´¹ä»ãã¾ããã SysV å½¢å¼ã® init ã¹ã¯ãªããï¼Debian Lenny ã å¤ã Ubuntuï¼ã®å ´åã¯ã MongoDB ããã¥ã¢ã«ã® Ubuntu and Debian packagesãåç
§ãã¦ãã ããã
æåã«ãä¸è¨ã®ã³ãã³ã㧠GPG ãã¼ãç»é²ãã¾ãã
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
ãã®å¾ã/etc/apt/source.list
ã«ä¸è¨ã追è¨ãããªãã¸ããªãç»é²ãã¾ãã
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
ä¸è¨ã³ãã³ã㧠MongoDB ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ sudo apt-get update
$ sudo apt-get install mongodb-10gen
Windows ã¸ã®ã¤ã³ã¹ãã¼ã«
Windows ã¸ã®ã¤ã³ã¹ãã¼ã«ã¯ãã¤ã³ã¹ãã¼ã©ãªã©ããªãããå°ãã ãä½æ¥ãå¿
è¦ã§ãã
MongoDB ãã¦ã³ãã¼ããã¼ã¸ ãããWindows ç¨ã®ãã¤ããªããã¦ã³ãã¼ããã¾ãã
解åããã¨ãmongodb-win32-XXXX-2.0.0ãã®ãããªãã©ã«ããåºæ¥ãã®ã§ããããé©å½ãªå ´æã«ç§»åããã¾ããï¼ããã§ã¯ãmongo ã«ãã©ã«ãåãå¤ããc:\mongo ã«è¨ç½®ãããã®ã¨ãã¾ããï¼
ãã¼ã¿ãã£ã¬ã¯ããªã®ä½æ
ã¨ã¯ã¹ããã¼ã©ãªã©ã§ DB ãã¼ã¿ãä¿åããããã£ã¬ã¯ããªãä½æãã¾ãã
c:\data\db
ãµã¼ãã¹ã¨ãã¦ç»é²ãã
Windowsãã¼ + R ã§ãcmd
ã¨å
¥åãã¦ã³ãã³ãããã³ããã表示ãã以ä¸ã®ã³ãã³ãã§ãµã¼ãã¹ã¨ãã¦ç»é²ãã¾ãã
c:\> cd c:\mongo\bin
c:\mongo\bin> mongod --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1
ãµã¼ãã¹ã¨ãã¦ç»é²ããããã以ä¸ã®ã³ãã³ã㧠MongoDB ãã¹ã¿ã¼ãããããã¨ãã§ãã¾ãã
c:\mongo\bin > net start "MongoDB"
MongoDB ã·ã§ã«ã¨æ¯ãã
ãã¦ã詳ããã¯æ¬¡åããç´¹ä»ãã¦ããã¾ãããããã§çµããã®ããªããªã®ã§ãå°ãã ã MongoDB ã·ã§ã«ã«è§¦ãã¦ä»åã¯çµãããã¨ã«ãã¾ãããã
MongoDB ã·ã§ã«ã®èµ·åã¨çµäº
>MongoDB ã·ã§ã«ã®èµ·åã¯ãmongo ã³ãã³ãã§è¡ãã¾ãã
$ mongo
ã³ãã³ããå®è¡ããã¨ãä¸è¨ã®ããã«è¡¨ç¤ºãã MongoDB ã·ã§ã«ã®ãã¼ã¸ã§ã³ 2.0.0 ãèµ·åããtest ãã¼ã¿ãã¼ã¹ã«æ¥ç¶ããããã¨ãåããã¾ãã
MongoDB shell version: 2.0.0
connecting to: test
>
ã¾ããMongoDB ã·ã§ã«ãçµäºããã«ã¯ exit ã³ãã³ããå ¥åãã¾ãã
exit
MongoDB ã·ã§ã«ã®ç¹å¾´
MongoDB ã·ã§ã«ã¯ Firefox ãªã©ã® Javascript ã¨ã³ã¸ã³ã§ãã SpiderMonkey ãæ¡å¼µãããJavascript ã·ã§ã«ã«ãªãã¾ããä»å¾ã®èª¬æã§ã¯ç¹ã«æè¨ããªãéãã¯ããã®ã·ã§ã«ä¸ã§ã®å®è¡ä¾ã¨ãªãã¾ãã
ã§ã¯ãã¾ãã·ã§ã«ä¸ã§ç°¡å㪠Javascript ãå®è¡ãã¦ã¿ããã¨æãã¾ãã
var data = []; // é åãåæå
for (var i = 0; i < 1000; i++) {
// é åã« num, text å¤ãæã£ããªãã¸ã§ã¯ãã追å
data.push({
num: i,
text: 'test' + i
});
}
ä¸è¨ã®ä¾ã§ã¯ãä½ã DB ã¸ã®æä½ããã¦ãã¾ããããJavascript ãå®è¡ããã¦ããããã
print(data.length);
print(data[100].text);
ã¨å®è¡ããã¨ã
1000
test100
ã¨ããããè¿ã£ã¦æ¥ãã¨æãã¾ãã
ãã®æ©è½ã¯ããã¹ããã¼ã¿ãæ¿å
¥ããå ´åãªã©ã«ãé常ã«ä¾¿å©ã«ä½¿ããã§ãããã
ã§ã¯ãå°ãã ã DB ã¸ã®æä½ããã¦ã¿ã¾ãããã
MongoDB ã®ã³ãã³ããã¯ã¨ãªãç¥ããªãã¦ãããã®é°å²æ°ãæãã¦ããã ããã°ã¨æãã¾ãã
ã¾ãã¯ããã¼ã¿ã®ä¿åã§ããå
ã»ã©ã®ãã¼ã¿ãä¿åãã¾ãã
use first_mongo; // DB ã®é¸æ
for (var i = 0; data.length; i++) {
db.test.save(data[i]);
}
4 è¡ç®ã§ä¿åããã¦ããã®ã MongoDB ãç¥ããªãã¦ãä½ã¨ãªãåãããã¨æãã¾ãã
次ã«ãã¼ã¿ããã¡ãã¨ä¿åããã¦ãããã確èªãããã¨æãã¾ããã¾ãã¯ããã¼ã¿ä»¶æ°ã確èªãã¦ã¿ã¾ãã
db.test.count();
æ£å¸¸ã«ä¿åããã¦ããã°ã1000
ã¨è¿ã£ã¦æ¥ãã¯ãã§ãã
å®éã«ä¿åããããã¼ã¿ã確èªãã¦ã¿ã¾ããããä¸è¨ã®ã³ãã³ãã§ä¿åããããã¼ã¿ãåå¾ãã¾ãã
db.test.find();
ããã¨ãä¸è¨ã®ããã«ä¿åããã¦ãããã¼ã¿ãè¿ã£ã¦æ¥ãã¯ãã§ããï¼ObjectId ã¨ããé¨åã®å¤ã¯ç°å¢ã«ãã£ã¦ç°ãªãã¾ããï¼
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d1ff"), "num" : 0, "text" : "test0" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d200"), "num" : 1, "text" : "test1" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d201"), "num" : 2, "text" : "test2" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d202"), "num" : 3, "text" : "test3" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d203"), "num" : 4, "text" : "test4" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d204"), "num" : 5, "text" : "test5" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d205"), "num" : 6, "text" : "test6" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d206"), "num" : 7, "text" : "test7" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d207"), "num" : 8, "text" : "test8" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d208"), "num" : 9, "text" : "test9" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d209"), "num" : 10, "text" : "test10" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d20a"), "num" : 11, "text" : "test11" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d20b"), "num" : 12, "text" : "test12" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d20c"), "num" : 13, "text" : "test13" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d20d"), "num" : 14, "text" : "test14" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d20e"), "num" : 15, "text" : "test15" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d20f"), "num" : 16, "text" : "test16" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d210"), "num" : 17, "text" : "test17" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d211"), "num" : 18, "text" : "test18" }
{ "_id" : ObjectId("4e78ab3e4e6d38925bd6d212"), "num" : 19, "text" : "test19" }
has more
ãã¼ã¿æ¿å
¥æã«æå®ãã¦ããªã "_id" ã¨ããå¤ãå®ç¾©ããã¦ãã以å¤ã¯ããã¼ã¿ãä¿åããã¦ããããã§ãã
ãªããMongoDB ã·ã§ã«ã§ã¯ 1 ç»é¢ã§ 20 件ã¾ã§ã®ãã¼ã¿ãã表示ãã¾ããããã®æ¬¡ã®ãã¼ã¿ãè¦ãããã«ã¯ it
ï¼iterate ã®ç¥ï¼ã¨å
¥åããäºã§ã次㮠20 件ã®ãã¼ã¿ã表示ãã¾ãã
ããã§ã¯ãä»æ¥ã¯ããã§çµããã§ãã®ã§ãéãã ãã¼ã¿ã«ã¤ãã¦ã¯åé¤ãã¦ãããã¨ã«ãã¾ãããã
use first_mongo;
db.dropDatabase(); // first_mongo ãã¼ã¿ãã¼ã¹ãåé¤
ã¾ã¨ã
ä»å㯠MongoDB ãã¤ã³ã¹ãã¼ã«ããå®éã« MongoDB ã·ã§ã«ã§ããã¤ãã®å¦çãè¡ã£ã¦ã¿ã¾ããã
ã¾ã ä½ã MongoDB ã«ã¤ãã¦èª¬æãã¦ããªããããªæ°ããã¾ããããã®é°å²æ°ã ãã§ãã¤ããã§é ããã°ã¨æãã¾ãã
ãã¦ã次åãã㯠MongoDB ã·ã§ã«ã使ã£ã¦ MongoDB ã®æ§ã
ãªæ©è½ã«å
¥ã£ã¦ããããã¨æãã¾ãã
é¢é£è¨äºãã©ãã
- 2.0 ããã¯ããã MongoDB
- ãçªå¤ç·¨ãMongoDB ã®æ´å²ï¼MongoDB 2.0 ã¾ã§ï¼