はじめてのMongoDB

23,769 views

Published on

 MongoDBの初心者向け資料です。SQLとの比較からMongoDBを理解する章を中心に展開していきます。また、Replication・Shardingに関しては別章で詳しく紹介します。
 この資料をきっかけに多くの方がMongoDBを知り、触れるきっかけになればと思います。

0 Comments
48 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
23,769
On SlideShare
0
From Embeds
0
Number of Embeds
8,757
Actions
Shares
0
Downloads
325
Comments
0
Likes
48
Embeds 0
No embeds

No notes for slide

はじめてのMongoDB

  1. 1. http://blog.nahurst.com/visual-guide-to-nosql-systems
  2. 2. { "_id" : ObjectId("4dcd3ebc9278000000005158"), "timestamp" : ISODate("2011-05-13T14:22:46.777Z"), "binary" : BinData(0,""), "string" : "abc", "number" : 3, "subobj" : {"subA": 1, "subB": 2 }, "array" : [1, 2, 3], "dbref" : [_id1, _id2, _id3]}
  3. 3. { "_id" : ObjectId("4dcd3ebc9278000000005158"), "timestamp" : ISODate("2011-05-13T14:22:46.777Z"), "binary" : BinData(0,""), "string" : "abc", "number" : 3, "subobj" : {"subA": 1, "subB": 2 }, "array" : [1, 2, 3], "dbref" : [_id1, _id2, _id3]}
  4. 4. { "_id" : ObjectId("4dcd3ebc9278000000005158"), "timestamp" : ISODate("2011-05-13T14:22:46.777Z"), "binary" : BinData(0,""), "string" : "abc", "number" : 3, "subobj" : {"subA": 1, "subB": 2 }, "array" : [1, 2, 3], padding}
  5. 5. { "_id" : ObjectId("4dcd3ebc9278000000005158"), "timestamp" : ISODate("2011-05-13T14:22:46.777Z"), "binary" : BinData(0,""), "string" : "def", "number" : 4, "subobj" : {"subA": 1, "subB": 2 }, "array" : [1, 2, 3, 4, 5, 6], "newkey" : "In-place"}
  6. 6. { "_id" : ObjectId("4dcd3ebc9278000000005158"), "timestamp" : ISODate("2011-05-13T14:22:46.777Z"), "binary" : BinData(0,""), "string" : "abc", "number" : 3, "subobj" : {"subA": 1, "subB": 2 }, "array" : [1, 2, 3],}
  7. 7. $ mongod --master --dbpath /data/masterdb/ --rest$ mongod --slave --source <masterhostname>[:<port>]--dbpath /data/slavedb/ --rest
  8. 8. $ mongod --replSet setname --rest> config = {_id: mySet, members: [ {_id: 0, host: host1:27017, priority: 1.0}, {_id: 1, host: host2:27018’, priority: 0.5}, {_id: 2, host: host3:27019, priority: 0.0} ] }> rs.initiate(config)> rs.status() //
  9. 9. Cluster Shard Servers (Data) config Servers (Shard Configration) shard1 shard2 shard3 [ a, f ) [ k, n) [ o, t ) Chunk [ f, k ) [ n, o ) [ t, } ) mongos Servers (Routers)
  10. 10. Shard 1 ( mongosprimary primary ) Shardcinfig Shard mongos
  11. 11. ~ mkdir -p shard/shard00~ mkdir -p shard/shard01~ mkdir -p shard/shard02~ mkdir -p shard/config~ mongod --shardsvr --port 27017 --dbpath shard/shard00~ mongod --shardsvr --port 27018 --dbpath shard/shard01~ mongod --shardsvr --port 27019 --dbpath shard/shard02~ mongod --configsvr --port 27020 --dbpath shard/config~ mongos --configdb localhost:27020 --port 27021
  12. 12. ➜ ~ mongo localhost:27021 // mongosMongoDB shell version: 1.8.0connecting to: localhost:27021/test> show dbsconfig 0.1875GB // sharding> db.adminCommand( { addshard: "localhost:27017", name: "shard00" } ){ "shardAdded" : "shard00", "ok" : 1 }> db.adminCommand( { addshard: "localhost:27018", name: "shard01" } ){ "shardAdded" : "shard01", "ok" : 1 }> db.adminCommand( { addshard: "localhost:27019", name: "shard02" } ){ "shardAdded" : "shard02", "ok" : 1 }
  13. 13. > db.adminCommand( { addshard: "set00/delta1:27017,delta2:27017", name:"shard00" } ){ "shardAdded" : "shard00", "ok" : 1 }> db.adminCommand( { addshard: "set01/delta3:27018,delta4:27018", name:"shard01" } ){ "shardAdded" : "shard01", "ok" : 1 }> db.adminCommand( { addshard: "set02/delta5:27019,delta6:27019", name:"shard02" } ){ "shardAdded" : "shard02", "ok" : 1 }
  14. 14. // mongos> db.adminCommand( { enablesharding : "test" } ){ "ok" : 1 }> db.adminCommand( { shardcollection : "test.myshard", key : { n : 1 } } ){ "collectionsharded" : "test.myshard", "ok" : 1 }
  15. 15. http://www.slideshare.net/doryokujin/mongo-sharding
  16. 16. http://www.mongodb.org/display/DOCSJP/Journaling
  17. 17. //> box = [[40.73083, -73.99756], [40.741404, -73.988135]]> db.places.find({"loc" : {"$within" : {"$box" : box}}})//> center = [50, 50]> radius = 10> db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})// v1.9> polygonA = [ [ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ] ]> polygonB = { a : { x : 10, y : 20 }, b : { x : 15, y : 25 }, c : { x :20, y : 20 } }> db.places.find({ "loc" : { "$within" : { "$polygon" : polygonA } } })> db.places.find({ "loc" : { "$within" : { "$polygon" : polygonB } } })
  18. 18. ➜ ~ mongofiles listconnected to: 127.0.0.1➜ ~ mongofiles put music1.mp3connected to: 127.0.0.1added file: { _id: ObjectId(4df17f8d9d47ba5c0247e72e),filename: "music1.mp3", chunkSize: 262144, uploadDate: newDate(1307672462538), md5:"9ee9472200a2e18bf376ce622c3b0055", length: 11183104 }done!➜ ~ mongofiles list -v // -vFri Jun 10 11:21:05 creating new connection to:127.0.0.1Fri Jun 10 11:21:05 BackgroundJob starting:connected to: 127.0.0.1music.mp3 11183104
  19. 19. ➜ ~ mongofiles put music2.mp3connected to: 127.0.0.1added file: { _id: ObjectId(4df181fc5e354129e833193f),filename: "music2.mp3", chunkSize: 262144, uploadDate: newDate(1307673086293), md5:"9d4f424fa1843711e196e502d8a00183", length: 12225353 }done!➜ ~ mongofiles listconnected to: 127.0.0.1music1.mp3 11183104music2.mp3 12225353➜ ~ mongofiles search .mp3connected to: 127.0.0.1music1.mp3 11183104music2.mp3 12225353
  20. 20. Map-Reduce on Mongo

×