Collection “product”:        Collection “order”:
{                            {
     "_id" : productId1,         "_id" : orderId,
     "name" : name1,             "user" : userInfo,
     "price" : price1,           "items" : [
     "desc" : description1           productId1,
},                                   productId2,
{                                    productId3
     "_id" : productId2,         ]
…                            }
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)
Collection “order”:
{
    "_id" : orderId,
    "user" : userInfo,
    "items" : [
          {
              "_id" : productId1,
              "name" : name1,
              "price" : price1
          }, {
              "_id" : productId1,
              "name" : name1,
              "price" : price2
          },...
    ] }
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 } }

          },...                     } } )

    ] }
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      }
          },...
    ] }
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”
    ] }
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
          },...
    ] }
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 }}
                                          ...
})
                                     }
> db.coll.stats()
{   ...
    #1

    "paddingFactor" : 1.4099999999940787,
    ...
}
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] }
}
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] }
}
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] }
}
> collection.insert(
   { "_id" : 123,
    /* other fields */,
    "garbage" : someLongString})
> collection.update(
                          insert   gabage
   { "_id": 123 },
   { $unset: { "garbage" : 1} })
#   :(       )

{
         "_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
                                         )
}
{
    "_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
                               })
}
{
    "_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
                                   })
}
# 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])
C

A       A               A
                B               B




    C       C               C
                    A
            B               B
C       C       C
                A
            B       B




                    C

A       A       A
            B       B
> 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
journaling


         :buildIndexes=false


 ReplicaSet             3
                  AWS
Single Instance
Slave Delay




              1
journaling
Micro Instance


 Small Instance
 32bit
 Large Instance


        Arbiter


Small
$ ls /dbpath/rollback
dbname.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
DC1      P
DC1                   S   P
      DC2
       Master
                DC2
> db.collname.insert({_id:...})
#
> db.getLastError()
# or
> db.runCommand("getlasterror")
> db.runCommand({ getlasterror: 1, fsync: true })
{ "err" : null, "n" : 0, "fsyncFiles" : 2, "ok" : 1 }




> db.runCommand({ getlasterror: 1, j: true })
> db.getLastError(2, 5000) // w=2, timeout 5000ms




> db.getLastError("majority")
Advanced MongoDB #1
Advanced MongoDB #1
Advanced MongoDB #1
Advanced MongoDB #1
Advanced MongoDB #1
Advanced MongoDB #1
Advanced MongoDB #1

Advanced MongoDB #1

  • 9.
    Collection “product”: Collection “order”: { { "_id" : productId1, "_id" : orderId, "name" : name1, "user" : userInfo, "price" : price1, "items" : [ "desc" : description1 productId1, }, productId2, { productId3 "_id" : productId2, ] … }
  • 10.
    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)
  • 11.
    Collection “order”: { "_id" : orderId, "user" : userInfo, "items" : [ { "_id" : productId1, "name" : name1, "price" : price1 }, { "_id" : productId1, "name" : name1, "price" : price2 },... ] }
  • 12.
    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 } } },... } } ) ] }
  • 13.
    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 } },... ] }
  • 14.
    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” ] }
  • 18.
    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 },... ] }
  • 19.
    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 }} ... }) }
  • 21.
    > db.coll.stats() { ... #1 "paddingFactor" : 1.4099999999940787, ... }
  • 23.
    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] } }
  • 24.
    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] } }
  • 25.
    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] } }
  • 26.
    > collection.insert( { "_id" : 123, /* other fields */, "garbage" : someLongString}) > collection.update( insert gabage { "_id": 123 }, { $unset: { "garbage" : 1} })
  • 30.
    # :( ) { "_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 ) }
  • 33.
    { "_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 }) }
  • 35.
    { "_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 }) }
  • 38.
    # 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])
  • 39.
    C A A A B B C C C A B B
  • 40.
    C C C A B B C A A A B B
  • 42.
    > 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
  • 47.
    journaling :buildIndexes=false ReplicaSet 3 AWS Single Instance
  • 48.
    Slave Delay 1 journaling
  • 49.
    Micro Instance SmallInstance 32bit Large Instance Arbiter Small
  • 51.
    $ ls /dbpath/rollback dbname.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
  • 57.
    DC1 P DC1 S P DC2 Master DC2
  • 59.
  • 60.
    > db.runCommand({ getlasterror:1, fsync: true }) { "err" : null, "n" : 0, "fsyncFiles" : 2, "ok" : 1 } > db.runCommand({ getlasterror: 1, j: true })
  • 61.
    > db.getLastError(2, 5000)// w=2, timeout 5000ms > db.getLastError("majority")