Tips, Trick and Hacks<br />Get’n it done!<br />
Basic Expectations<br />No transactions<br />Fast-N-Loose by default (no safe/w/GLE)<br />Indexing order matters; query op...
Indexes<br />Index on A, B, C works for [A], [A,B], [A,B,C]<br />Query Optimizer figures out order<br />Hint when you know...
Shell: Functions<br />Leave off the () to see the function:<br />> db.coll.find<br />function (query, fields, limit, skip)...
Connections<br />Order of ops is only preserved in the same connection (socket/port)<br />Connection pools are good, but c...
getLastError()<br />More like getLastOpStatus()<br />Returns useful data on update/findAndModify/insert/remove ops<br />> ...
getLastErrorParams<br />Params<br />w: number of replicas to write to<br />wtimeout: time to wait for acknowledgements<br ...
Shell Command Line<br />--eval<br />it/cursor<br />Printing values; be careful<br />Pass in a script<br />
Enable Profiling<br />setProfilingLevel(lvl, <ms>)<br />0: none<br />1: time-based<br />2: all<br />Reading from profile c...
_id: ObjectId Generated on Client<br />Most drivers create “_id” field (if not set)<br />No way to get “_id” after insert ...
mongod.conf<br />Specify a logpath or it goes to /dev/null<br />Quiet = true/anything<br />Don’t do Quiet = false<br />Fla...
Limit/Sort/Pagination<br />Sort fields should be the last in the index<br />Limit helps with in-memory sorts (diff alg.)<b...
Bulk Data Loading<br />Initial load<br />Create the index after load<br />Start with new DB if possible<br />Updates<br />...
Printing Collection Stats<br />db.getCollectionNames().<br />forEach(function(x){<br />	print(“Collection: “ + x);<br />pr...
Backups<br />Only way to get a consistent state<br />Don’t use mongoexport (lacks type fidelity)<br />Use mongodump (snap-...
mongostat<br />Watch <br />% idx miss<br />faults/sec<br />flushes/sec<br />
Questions<br />You don’t have to go home, but you can’t…<br />ScottHernandez@gmail.com<br />
Upcoming SlideShare
Loading in...5
×

MongoDB: tips, trick and hacks

13,555

Published on

Published in: Technology
1 Comment
19 Likes
Statistics
Notes
No Downloads
Views
Total Views
13,555
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
284
Comments
1
Likes
19
Embeds 0
No embeds

No notes for slide

MongoDB: tips, trick and hacks

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

    Clipping is a handy way to collect important slides you want to go back to later.

×