Your SlideShare is downloading. ×

Shell Tips & Tricks

855

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
855
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
38
Comments
0
Likes
1
Embeds 0
No embeds

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

    ×