• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
MongoDB: tips, trick and hacks
 

MongoDB: tips, trick and hacks

on

  • 13,116 views

 

Statistics

Views

Total Views
13,116
Views on SlideShare
7,875
Embed Views
5,241

Actions

Likes
19
Downloads
260
Comments
1

33 Embeds 5,241

http://blog.nosqlfan.com 2397
http://cloud.csdn.net 949
http://nosql.mypopescu.com 933
http://www.csdn.net 344
http://liminescence.blogspot.com 223
http://www.iwanna.cn 204
http://woshao.com 65
http://web.vabx.com 26
http://fe.com 24
http://www.lifeyun.com 18
http://www.w3ccollege.org 13
http://fabbrikk.tumblr.com 6
http://translate.googleusercontent.com 4
http://static.slidesharecdn.com 4
http://liminescence.blogspot.ca 4
http://liminescence.blogspot.co.uk 3
http://liminescence.blogspot.in 3
http://webcache.googleusercontent.com 2
http://webcache.googleusercontent.com 2
http://article.woshao.com 2
http://m.csdn.net 2
http://articles.csdn.net 2
http://liminescence.blogspot.ie 1
http://liminescence.blogspot.de 1
http://liminescence.blogspot.jp 1
http://cncc.bingj.com 1
http://flavors.me 1
http://usekit.com 1
http://codebetter.com 1
resource://brief-content 1
http://www.hanrss.com 1
http://feeds.feedburner.com 1
http://xue.uplook.cn 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    MongoDB: tips, trick and hacks MongoDB: tips, trick and hacks Presentation Transcript

    • Tips, Trick and Hacks
      Get’n it done!
    • Basic Expectations
      No transactions
      Fast-N-Loose by default (no safe/w/GLE)
      Indexing order matters; query optimizer helps
      One write thread
      One JS thread (M/R, $where, group)
      Many query threads (non-js)
      Memory Mapped Data Files
      BSON everywhere
    • Indexes
      Index on A, B, C works for [A], [A,B], [A,B,C]
      Query Optimizer figures out order
      Hint when you know
      Missing values are indexed as “null” value
      Just like real “null” values
      Unique indexes include missing/nulls
      Sort works on last field
    • Shell: Functions
      Leave off the () to see the function:
      > db.coll.find
      function (query, fields, limit, skip) {
      return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip); }
    • Connections
      Order of ops is only preserved in the same connection (socket/port)
      Connection pools are good, but could be a problem (getLastError/ordered ops)
      Server only executes one (concurrent) operation per connection
    • getLastError()
      More like getLastOpStatus()
      Returns useful data on update/findAndModify/insert/remove ops
      > db.t.update({x:1}, {$inc:{y:1}}, true, true)
      > db.getLastErrorObj()
      { "err" : null,
      "updatedExisting" : false,
      "upserted" : ObjectId("4c49f54cab620000000071b7"),
      "n" : 1,
      "ok" : 1 }
    • getLastErrorParams
      Params
      w: number of replicas to write to
      wtimeout: time to wait for acknowledgements
      fsync: flush to disk
      {getlasterror : 1, w : 40, wtimeout: 3000}
      { "err" : null, "n" : 0,
      "wtimeout" : true, "waited" : 3006,
      "errmsg" : "timed out waiting for slaves", "ok" : 0 }
    • Shell Command Line
      --eval
      it/cursor
      Printing values; be careful
      Pass in a script
    • Enable Profiling
      setProfilingLevel(lvl, <ms>)
      0: none
      1: time-based
      2: all
      Reading from profile collection
      >db.system.profile.find()
    • _id: ObjectId Generated on Client
      Most drivers create “_id” field (if not set)
      No way to get “_id” after insert (from server)
      Nothing you can’t do yourself
      class Foo {
      ObjectId id = new ObjectId()
      ….
      }
    • mongod.conf
      Specify a logpath or it goes to /dev/null
      Quiet = true/anything
      Don’t do Quiet = false
      Flag options used no matter what values
      V[v*] = true for verbose logging
    • Limit/Sort/Pagination
      Sort fields should be the last in the index
      Limit helps with in-memory sorts (diff alg.)
      Skip/Offset still walks through index
      Pagination is best using last “_id”/sort value
      Set batchsize = pagesize (under 4MB)
    • Bulk Data Loading
      Initial load
      Create the index after load
      Start with new DB if possible
      Updates
      Be careful of excessive indexes
      Sometimes better to drop and recreate indexes
    • Printing Collection Stats
      db.getCollectionNames().
      forEach(function(x){
      print(“Collection: “ + x);
      printjson(db[x].stats());
      })
    • Backups
      Only way to get a consistent state
      Don’t use mongoexport (lacks type fidelity)
      Use mongodump (snap-shotted query)
      Or fsync+copy
      1.) fsync + lock
      2.) backup dbpath files
      3.) release lock
    • mongostat
      Watch
      % idx miss
      faults/sec
      flushes/sec
    • Questions
      You don’t have to go home, but you can’t…
      ScottHernandez@gmail.com