Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Advanced MongoDB #1

9,115 views

Published on

MongoDBのTipsや運用における注意点など、一歩進んだ内容を紹介します。書ききれないので#1です。

Published in: Technology, Business
  • Be the first to comment

Advanced MongoDB #1

  1. 1. Collection “product”: Collection “order”:{ { "_id" : productId1, "_id" : orderId, "name" : name1, "user" : userInfo, "price" : price1, "items" : [ "desc" : description1 productId1,}, productId2,{ productId3 "_id" : productId2, ]… }
  2. 2. Collection “product”: Collection “order”:{ { "_id" : productId1, "_id" : orderId, "name" : name1, "user" : userInfo, "price" : price1, "items" : [ "desc" : description1 productId1,} productId2,{ productId3> db.product.findOne(“_id”:productId1) "_id" : productId2, ]> db.product.findOne(“_id”:productId2)… }> db.product.findOne(“_id”:productId3)
  3. 3. Collection “order”:{ "_id" : orderId, "user" : userInfo, "items" : [ { "_id" : productId1, "name" : name1, "price" : price1 }, { "_id" : productId1, "name" : name1, "price" : price2 },... ] }
  4. 4. Collection “order”:{ order "_id" : orderId, "user" : userInfo, "items" : [ collection name { price order "_id" : productId1, "name" : name1, > db.order.find({ items :{ "price" : price1 $elemMatch: { name: name1 } }, { } } ) "_id" : productId1, "name" : name1, > db.order.find({ items :{ "price" : price2 $elemMatch: { price:{ $gt: 3000 } } },... } } ) ] }
  5. 5. Collection “order”: Collection “performance_rating”:{ { "_id" : orderId, "_id" : "employeeId", "user" : userInfo, "name" : "name", "items" : [ "post" : "section chief", { "abilities" : { "_id" : productId1, "skillA" : 20, "name" : name1, "skillC" : 12, "price" : price1 "skillE" : 18, }, { "skillF" : 24, "_id" : productId1, "skillG" : 23 "name" : name1, } "price" : price2 } },... ] }
  6. 6. Collection “order”: Collection “performance_rating”:{ { "_id" : orderId, "_id" : "employeeId",$elemMatch "user" : userInfo, "name" : "name", "items" : [ "post" : "section chief", { "abilities" : { "_id" : productId1, "skillA" : 20, "name" : name1, "skillC" : 12, "price" : price1 "skillE" : 18, }, { "skillF" : 24, "_id" : productId1, "skillG" : 23 "name" : name1, } 1 "price" : price2 } },... ”abilities.skillA” ] }
  7. 7. Collection “blog”:{ comment "_id" : blogId, "user" : userInfo, "tags" : [tag1, tag2, tag3] 16MB/document "comment": [{ In-Place "_id" : commentId1, "user" : userInfo1, "contents" : contents1 }, { "_id" : commentId2, "user" : userInfo2, "contents" : contents2 },... ] }
  8. 8. Collection “comment”:Collection “blog”: {{ "_id" : commentId, "_id" : blogId, "user" : userInfo, "user" : userInfo, > "contents" : contents1, db.comment.insert({ "tags" : [tag1, tag2, tag3] “_id”:commentId5, "lastUpdate" : datetime, "comment" : [ “user”:... ... commentId1, }) },... commentId3, { commentId5 "_id" : commentId5, ],... "user" : userInfo, }, "contents" : contents1,> db.blog.update({“_id”:blogId}, { "lastUpdate" : datetime,{$push: { “comment”: commentId5 }} ...}) }
  9. 9. > db.coll.stats(){ ... #1 "paddingFactor" : 1.4099999999940787, ...}
  10. 10. Collection “access_history”:{ "_id" : pageId, “start” 6 "start" : time, visitor "visits" : { "minutes" : [ “minites” [] [num0, num1, ..., num59], ( ) [num0, num1, ..., num59], [num0, num1, ..., num59], “minites” ”hours” [num0, num1, ..., num59], 0 [num0, num1, ..., num59], [num0, num1, ..., num59] ], "hours" : [num0, ..., num5] }}
  11. 11. Collection “access_history”:{ "_id" : pageId, "start" : time, "visits" : { "minutes" : [ [0,0, ..., 0], [0,0, ..., 0], [0,0, ..., 0], [0,0, ..., 0], [0,0, ..., 0], [0,0, ..., 0] ], "hours" : [0, ..., 0] }}
  12. 12. Collection “access_history”:{ "_id" : pageId, $inc "start" : time, "visits" : { "minutes" : [ [0,0, ..., 0], [3,0, ..., 0], > db.pages.update({ [0,0, ..., 0], "_id" : pageId, [0,0, ..., 0], "start" : thisHour },{ [0,0, ..., 0], "$inc": {"visits.minutes.1.0": 3} [0,0, ..., 0] }) ], "hours" : [0, ..., 0] }}
  13. 13. > collection.insert( { "_id" : 123, /* other fields */, "garbage" : someLongString})> collection.update( insert gabage { "_id": 123 }, { $unset: { "garbage" : 1} })
  14. 14. # :( ){ "_id" : BinData(5,"iiOI5R6P+dbut19O1mS1lA=="), "reduce_id" : 0, _id = user_id "user_id" : 00000, + date "date" : 20110727, + timeMilliSec + action_type "action_type" : "a{Make}", "key_depth" : 1, _id "action_detail" : { Binary "kakejiku" : 2, # Python "kaizokunojyuu" : 1, bson.Binary( "matoryo-sika" : 1, hashlib.md5( str(unique_id) "syokudai" : 1 ).digest(), } bson.binary.MD5_SUBTYPE )}
  15. 15. { "_id" : id, "name" : username, "email" : email, "phone" : phone, "twitter" : username, ”fax” "facebook" : username, "linkedin" : username, "google+" : number, "street" : street "city" : city, "state" : state, "zip" : zip, > db.profile.find({ "fax" : number “fax”: number })}
  16. 16. { "_id" : id, "name" : username, “online”: { "email" : email, "twitter" : username, ... }, ”address”: { ”telephone” "street" : street "city" : city, ... }, “telephone”: { "phone" : phone, > db.profile.find({ "fax" : number “telephone.fax”: number })}
  17. 17. # A B C> db.coll.find({“A”: a, “B”: b, “C”: c})# C B A> db.coll.find({“C”: c, “B”: b, “A”: a})# A B C> db.coll.find($or: [“A”: a, “B”: b, “C”: c])# C B A> db.coll.find($or: [“C”: c, “B”: b, “A”: a])
  18. 18. CA A A B B C C C A B B
  19. 19. C C C A B B CA A A B B
  20. 20. > db.coll.ensureIndex({ A: 1 })> db.coll.ensureIndex({ B: 1 })> db.coll.ensureIndex({ C: 1 })# find 2 “Select A,B,C”> db.coll.find({ A: a, B: b, C: c }, { _id: -1, A: 1, B: 1, C: 1 }) (_id ) Covered Index
  21. 21. journaling :buildIndexes=false ReplicaSet 3 AWSSingle Instance
  22. 22. Slave Delay 1journaling
  23. 23. Micro Instance Small Instance 32bit Large Instance ArbiterSmall
  24. 24. $ ls /dbpath/rollbackdbname.collname.2011-08-14T18-27-14.0.bson$ bsondump dbname.collname.2011-08-14T18-27-14.0.bson { "_id" : ... } { "_id" : ... } { "_id" : ... } Wed Aug 15 13:33:32 3 objects found
  25. 25. DC1 PDC1 S P DC2 Master DC2
  26. 26. > db.collname.insert({_id:...})#> db.getLastError()# or> db.runCommand("getlasterror")
  27. 27. > db.runCommand({ getlasterror: 1, fsync: true }){ "err" : null, "n" : 0, "fsyncFiles" : 2, "ok" : 1 }> db.runCommand({ getlasterror: 1, j: true })
  28. 28. > db.getLastError(2, 5000) // w=2, timeout 5000ms> db.getLastError("majority")

×