Shell Tips & Tricks

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,132
On Slideshare
975
From Embeds
157
Number of Embeds
5

Actions

Shares
Downloads
28
Comments
0
Likes
1

Embeds 157

http://www.10gen.com 137
http://www.mongodb.com 12
http://drupal1.10gen.cc 6
https://comwww-drupal.10gen.com 1
https://www.mongodb.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • ----- Meeting Notes (1/16/13 17:30) -----anecdote
  • Show Demo #1
  • Show Demo #2
  • See implementation by omitting parens()
  • do demo #3
  • do demo #4

Transcript

  • 1. #MongoDBDaysMongoDB Shell Tips &TricksMike FriedmanPerl Engineer & Evangelist, 10gen
  • 2. What is the Shell? Embedded • vars Javascript • functions Interpreter • data structs + types • 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
  • 3. MongoDB Shell: Advantages• Debugging Queries / Syntax• Testing• Administration• Scripting Glue
  • 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
  • 5. 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
  • 6. Insert, Update, Removefor ( i = 0; i < 1000; i++ ) { db.test.insert( { x: i, ts: new Date() } );}
  • 7. Loading Scripts• Commandline – --eval switch – .js files• Within the shell – load()
  • 8. Running Commands• db.runCommand( { ... } ) – Runs any arbitrary command against the current DB• db.adminCommand( { ... } ) – Run commands against the admin database
  • 9. db.adminCommandDefinitionfunction (obj) { if (this._name == "admin") { return this.runCommand(obj); } return this.getSiblingDB("admin") .runCommand(obj);}
  • 10. Profiling• setProfilingLevel( lvl, <ms> ) – 0: none – 1: time-based – 2: all• getProfilingLevel()• Reading from the profile collection – db.system.profile.find()
  • 11. Cool Functions• printjson  tojson• forEach on arrays, queries, and cursors
  • 12. forEach example[{x:1},{y:1}].forEach(function(x) { printjson(x)}){ "x" : 1 }{ "y" : 1 }
  • 13. Cool Functions• printjson  tojson• forEach on arrays, queries, and cursors• Object.bsonsize• load(file)• run(file)
  • 14. Print all Indexesdb.getCollectionNames().forEach( function(x) { print( "Collection: " + x ); printjson( db[x].getIndexes() );})
  • 15. 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; }});
  • 16. #MongoDBDaysThank YouMike FriedmanPerl Engineer & Evangelist, 10gen