MongoDB Shell Tips & Tricks

4,623 views
4,087 views

Published on

1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
4,623
On SlideShare
0
From Embeds
0
Number of Embeds
86
Actions
Shares
0
Downloads
36
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • http://try.mongodb.org/– it is almost a full clone of the shellThe shell is your buddy, your goto tool for quick hacking/tasksIt is important to know and bridges everyone using MongoDB as it doesn’t include any of the language specific cruft or dependencies– just raw MongoDB :D !
  • Doesn’t matter which language you use, everyone else knows the shellAll examples during this conference are based on the shell
  • ----- Meeting Notes (1/16/13 17:30) -----anecdote
  • Unfortunately, we hire a lot of Brown students who apparently learn emacs :(No vi bindings… YET!
  • Unfortunately, we hire a lot of Brown students who apparently learn emacs :(No vi bindings… YET!
  • Believe it or not, show tables works too :PIf you care what is behind it, serverBuildInfo.interpreterVersionIt’s the common ground between the languages
  • Show Demo #1
  • Show Demo #2
  • See implementation by omitting parens()
  • do demo #3
  • do demo #4
  • MongoDB Shell Tips & Tricks

    1. 1. #MongoDBDaysMongoDB Shell Tips &TricksHannes MagnussonDriver Engineer, 10gen@bjori
    2. 2. What is the Shell? Embedded • vars / functions / data structs + types Javascript • Spidermonkey / V8 Interpreter • ObjectId("...")Global Functions • new Date() and Objects • Object.bsonsize()MongoDB driver • db["collection"].find/count/update Exposed • short-hand for collections • Doesnt require quoted keysJSON-like stuff • Don’t copy and paste too much Shell Tips & Tricks, Hannes Magnusson
    3. 3. MongoDB Shell: Advantages• Your goto tool• Debugging Queries / Syntax• Testing• Administration• Scripting Glue• You don’t do rs.stepDown() from your application Shell Tips & Tricks, Hannes Magnusson
    4. 4. MongoDB Shell: Disadvantages• Numbers in JS are a pain – 32/64-bit int/long  NumberInt() / NumberLong() – Primitive numbers are all 64-bit FP doubles• Dates can be confusing – new Date( "1/1/1" ) – new ISODate( ... ) NOT: Date( "1/1/1" )  string Shell Tips & Tricks, Hannes Magnusson
    5. 5. Emacs-like shell bindings ctrl A Move cursor to start of line ctrl E Move cursor to end of line meta B Move cursor left by one word meta F Move cursor right by one word ctrl L Clear screen and redisplay line ctrl R Reverse history search meta < Start of history meta > End of history Shell Tips & Tricks, Hannes Magnusson
    6. 6. Emacs-like shell bindings ctrl A Move cursor to start of line ctrl E Move cursor to end of line meta B Move cursor left by one word meta F Move cursor right by one word ctrl L Clear screen and redisplay line ctrl R Reverse history search meta < Start of history meta > End of history Shell Tips & Tricks, Hannes Magnusson
    7. 7. Easy to use• Tab completion on most objects• Built-in help on most objects (.help())• show – profile # 5 most recent ops of 1ms or more – users # List all the users of the current db – dbs # List all the databases – logs # List all available logs• Most examples use the shell Shell Tips & Tricks, Hannes Magnusson
    8. 8. Speed Considerations• Shell – JavaScript is slow – Always in "write-acknowledged" (safe mode) / GLE – Data Conversions• Server – Applies on the server as well – Careful with round-tripping numbers Shell Tips & Tricks, Hannes Magnusson
    9. 9. Insert, Update, Removefor ( i = 0; i < 1000; i++ ) { db.test.insert( { x: i, ts: new Date() } );} Shell Tips & Tricks, Hannes Magnusson
    10. 10. Loading Scripts• Commandline – --eval switch – .js files• Within the shell – load() Shell Tips & Tricks, Hannes Magnusson
    11. 11. Running Commands• db.runCommand( { ... } ) – Runs any arbitrary command against the current DB• db.adminCommand( { ... } ) – Run commands against the admin database Shell Tips & Tricks, Hannes Magnusson
    12. 12. db.adminCommandDefinitionfunction (obj) { if (this._name == "admin") { return this.runCommand(obj); } return this.getSiblingDB("admin") .runCommand(obj);} Shell Tips & Tricks, Hannes Magnusson
    13. 13. Profiling• setProfilingLevel( lvl, <ms> ) – 0: none – 1: time-based – 2: all• getProfilingLevel()• Reading from the profile collection – db.system.profile.find() Shell Tips & Tricks, Hannes Magnusson
    14. 14. Cool Functions• printjson()  tojson• forEach on arrays, queries, and cursors Shell Tips & Tricks, Hannes Magnusson
    15. 15. You didn’t hear it from medb.system.js.save({ _id:"echoFunction", value : function(x) { return x; }})db.loadServerScripts();echoFunction(3); Shell Tips & Tricks, Hannes Magnusson
    16. 16. forEach example[{x:1},{y:1}].forEach(function(x) { printjson(x)}){ "x" : 1 }{ "y" : 1 } Shell Tips & Tricks, Hannes Magnusson
    17. 17. Cool Functions• printjson  tojson• forEach on arrays, queries, and cursors• Object.bsonsize• load(file)• run(file) Shell Tips & Tricks, Hannes Magnusson
    18. 18. Print all Indexesdb.getCollectionNames().forEach( function(x) { print( "Collection: " + x ); printjson( db[x].getIndexes() );}) Shell Tips & Tricks, Hannes Magnusson
    19. 19. Getting the Biggest Docvar cursor = db.coll.find();var biggest = 0;var doc = {};cursor.forEach(function (x) { var size = Object.bsonsize(x); if (size > biggest) { biggest = size; doc = x; }}); Shell Tips & Tricks, Hannes Magnusson
    20. 20. Administration functions• Sharding – sh.status() – sh.enableSharding(dbname)• Replicaset – rs.status() – cfg = rs.config(); rs.reconfig(cfg); – db.isMaster()• Status – Object.bsonsize(document) – db.collectionName.stats() Shell Tips & Tricks, Hannes Magnusson
    21. 21. .mongorc.js• Automagically loaded on startup – Unless you specify --norc• Script your command prompt – prompt=function() { return "Hello World"; }• Colorize output• Move complex aggregate() queries into functions• Can be a directory Shell Tips & Tricks, Hannes Magnusson
    22. 22. Want to know more? The shell is self documented, in JavaScript o/ …Except the native helper• help – dbs # Shows all commands you can run on a database – connect # Connect to other nodes• Call a function, without the brackets – Will show you the actual code behind it• try.mongodb.org ! Shell Tips & Tricks, Hannes Magnusson
    23. 23. #MongoDBDaysThank YouHannes MagnussonDriver Engineer, 10gen@bjori

    ×