Tips, Trick and HacksGet’n it done!
Basic ExpectationsNo transactionsFast-N-Loose by default (no safe/w/GLE)Indexing order matters; query optimizer helpsOne write threadOne JS thread (M/R, $where, group)Many query threads (non-js)Memory Mapped Data FilesBSON everywhere
IndexesIndex on A, B, C works for [A], [A,B], [A,B,C]Query Optimizer figures out orderHint when you knowMissing values are indexed as “null” valueJust like real “null” valuesUnique indexes include missing/nullsSort works on last field
Shell: FunctionsLeave off the () to see the function:> db.coll.findfunction (query, fields, limit, skip) {    return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip); }
ConnectionsOrder 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 }
getLastErrorParamsParamsw: number of replicas to write towtimeout: time to wait for acknowledgementsfsync: 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--evalit/cursorPrinting values; be carefulPass in a script
Enable ProfilingsetProfilingLevel(lvl, <ms>)0: none1: time-based2: allReading from profile collection>db.system.profile.find()
_id: ObjectId Generated on ClientMost drivers create “_id” field (if not set)No way to get “_id” after insert (from server)Nothing  you can’t do yourselfclass Foo {ObjectId id = new ObjectId()   ….}
mongod.confSpecify a logpath or it goes to /dev/nullQuiet = true/anythingDon’t do Quiet = falseFlag options used no matter what valuesV[v*] = true for verbose logging
Limit/Sort/PaginationSort fields should be the last in the indexLimit helps with in-memory sorts (diff alg.)Skip/Offset still walks through indexPagination is best using last “_id”/sort valueSet batchsize = pagesize (under 4MB)
Bulk Data LoadingInitial loadCreate the index after loadStart with new DB if possibleUpdatesBe careful of excessive indexesSometimes better to drop and recreate indexes
Printing Collection Statsdb.getCollectionNames().forEach(function(x){	print(“Collection: “ + x);printjson(db[x].stats());})
BackupsOnly way to get a consistent stateDon’t use mongoexport (lacks type fidelity)Use mongodump (snap-shotted query)Or fsync+copy1.) fsync + lock2.) backup dbpath files3.) release lock
mongostatWatch % idx missfaults/secflushes/sec
QuestionsYou don’t have to go home, but you can’t…ScottHernandez@gmail.com

MongoDB: tips, trick and hacks

  • 1.
    Tips, Trick andHacksGet’n it done!
  • 2.
    Basic ExpectationsNo transactionsFast-N-Looseby default (no safe/w/GLE)Indexing order matters; query optimizer helpsOne write threadOne JS thread (M/R, $where, group)Many query threads (non-js)Memory Mapped Data FilesBSON everywhere
  • 3.
    IndexesIndex on A,B, C works for [A], [A,B], [A,B,C]Query Optimizer figures out orderHint when you knowMissing values are indexed as “null” valueJust like real “null” valuesUnique indexes include missing/nullsSort works on last field
  • 4.
    Shell: FunctionsLeave offthe () to see the function:> db.coll.findfunction (query, fields, limit, skip) { return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip); }
  • 5.
    ConnectionsOrder of opsis 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
  • 6.
    getLastError()More like getLastOpStatus()Returnsuseful 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 }
  • 7.
    getLastErrorParamsParamsw: number ofreplicas to write towtimeout: time to wait for acknowledgementsfsync: 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 }
  • 8.
    Shell Command Line--evalit/cursorPrintingvalues; be carefulPass in a script
  • 9.
    Enable ProfilingsetProfilingLevel(lvl, <ms>)0:none1: time-based2: allReading from profile collection>db.system.profile.find()
  • 10.
    _id: ObjectId Generatedon ClientMost drivers create “_id” field (if not set)No way to get “_id” after insert (from server)Nothing you can’t do yourselfclass Foo {ObjectId id = new ObjectId() ….}
  • 11.
    mongod.confSpecify a logpathor it goes to /dev/nullQuiet = true/anythingDon’t do Quiet = falseFlag options used no matter what valuesV[v*] = true for verbose logging
  • 12.
    Limit/Sort/PaginationSort fields shouldbe the last in the indexLimit helps with in-memory sorts (diff alg.)Skip/Offset still walks through indexPagination is best using last “_id”/sort valueSet batchsize = pagesize (under 4MB)
  • 13.
    Bulk Data LoadingInitialloadCreate the index after loadStart with new DB if possibleUpdatesBe careful of excessive indexesSometimes better to drop and recreate indexes
  • 14.
  • 15.
    BackupsOnly way toget a consistent stateDon’t use mongoexport (lacks type fidelity)Use mongodump (snap-shotted query)Or fsync+copy1.) fsync + lock2.) backup dbpath files3.) release lock
  • 16.
    mongostatWatch % idxmissfaults/secflushes/sec
  • 17.
    QuestionsYou don’t haveto go home, but you can’t…ScottHernandez@gmail.com