MongoDB Shell Tips & Tricks
 

MongoDB Shell Tips & Tricks

on

  • 2,334 views

 

Statistics

Views

Total Views
2,334
Views on SlideShare
2,260
Embed Views
74

Actions

Likes
1
Downloads
14
Comments
0

5 Embeds 74

http://www.10gen.com 65
http://www.mongodb.com 5
http://drupal1.10gen.cc 2
https://twitter.com 1
https://comwww-drupal.10gen.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 MongoDB Shell Tips & Tricks Presentation Transcript

  • #MongoDBDaysMongoDB Shell Tips &TricksHannes MagnussonDriver Engineer, 10gen@bjori
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Insert, Update, Removefor ( i = 0; i < 1000; i++ ) { db.test.insert( { x: i, ts: new Date() } );} Shell Tips & Tricks, Hannes Magnusson
  • Loading Scripts• Commandline – --eval switch – .js files• Within the shell – load() Shell Tips & Tricks, Hannes Magnusson
  • 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
  • db.adminCommandDefinitionfunction (obj) { if (this._name == "admin") { return this.runCommand(obj); } return this.getSiblingDB("admin") .runCommand(obj);} Shell Tips & Tricks, Hannes Magnusson
  • 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
  • Cool Functions• printjson()  tojson• forEach on arrays, queries, and cursors Shell Tips & Tricks, Hannes Magnusson
  • 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
  • forEach example[{x:1},{y:1}].forEach(function(x) { printjson(x)}){ "x" : 1 }{ "y" : 1 } Shell Tips & Tricks, Hannes Magnusson
  • Cool Functions• printjson  tojson• forEach on arrays, queries, and cursors• Object.bsonsize• load(file)• run(file) Shell Tips & Tricks, Hannes Magnusson
  • Print all Indexesdb.getCollectionNames().forEach( function(x) { print( "Collection: " + x ); printjson( db[x].getIndexes() );}) Shell Tips & Tricks, Hannes Magnusson
  • 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
  • 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
  • .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
  • 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
  • #MongoDBDaysThank YouHannes MagnussonDriver Engineer, 10gen@bjori