Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Advanced Shell Tips and Tricks
Search
mongodb
April 20, 2012
330
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Advanced Shell Tips and Tricks
MongoDB Stockholm 2012 - Advanced Shell Tips and Tricks - Spencer Brody, Engineer, 10gen
mongodb
April 20, 2012
More Decks by mongodb
See All by mongodb
NoSQL Now! 2012
mongodb
18
3.4k
MongoDB 2.2 At the Silicon Valley MongoDB User Group
mongodb
9
1.5k
Turning off the LAMP Hunter Loftis, Skookum Digital Works
mongodb
2
1.6k
Mobilize Your MongoDB! Developing iPhone and Android Apps in the Cloud Grant Shipley, Red Hat
mongodb
0
580
Beanstalk Data - MongoDB In Production Chris Siefken, CTO Beanstalk Data
mongodb
0
590
New LINQ support in C#/.NET driver Robert Stam, 10gen
mongodb
9
41k
Welcome and Keynote Aaron Heckman, 10gen
mongodb
0
560
Webinar Introduction to MongoDB's Java Driver
mongodb
1
1.3k
Webinar Intro to Schema Design
mongodb
4
1.8k
Featured
See All Featured
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
710
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.5k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
67
57k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
480
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
220
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
4 Signs Your Business is Dying
shpigford
187
23k
Six Lessons from altMBA
skipperchong
29
4.5k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
410
Evolving SEO for Evolving Search Engines
ryanjones
0
260
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
220
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Transcript
MASTERING THE SHELL Spencer Brody –
[email protected]
@stbrody
What is the shell? • vars • functions • data
structs + types Embedded Javascript Interpreter • ObjectId("...") • new Date() • Object.bsonsize() Global Functions and Objects • db["collection"].find/count/update • short-hand for collections MongoDB driver Exposed • Doesn't require quoted keys • Don’t copy and paste too much JSON-like stuff
What is it good for? ¨ Interactive development/prototyping ¨ Debugging
¨ Test scripting (ex. MongoDB’s own regression tests) ¨ Administrative operations, lightweight scripting ¨ Learning (and teaching) MongoDB ¨ Not building apps, probably
Interactive mode ¨ Demo now J ¨ for(i = 0;
i <1000; i++) { db.test.insert({x:i, ts: new Date()}) }
Command Line ¨ --eval ¤ Printing values ¨ Pass in
a script
Loading Scripts ¨ Command line ¨ load() – also runs
The Bad: JS Types ¨ Numbers Suck (but getting better)
¤ 32/64bit signed (int/long) – 1.5.4>NumberLong(“”) ¤ Displayed funny ¤ Everything is a 64bit fp (double) ¨ Dates are a challenge ¤ new Date(“1/1/1”) ¤ Not Date(“1/1/1”) -> string
The Bad: JS is Slow ¨ Shell ¤ Safe/GLE ¤
Loops and updates ¤ Data conversions ¨ Server ¤ It pretty much applies here too ¤ Be careful with numbers as well
Shell helpers ¨ Show collections/dbs ¨ Use <db>
Cursors ¨ Cursors printed by iterating and printing 20 values
¨ find automatically sets “it” global var ¨ Cursors as first-class objects
Run Commands ¨ db.runCommand({…}) ¨ db.runCommand(“getLastError”) ¨ db.adminCommand({…})
Useful Commands ¨ getCmdLineOpts ¨ ping ¨ isMaster ¨ reIndex
¨ sharding ¨ replication
Help > help > help admin > help misc >
db.help() > db.coll.help()
Expose Functions ¨ Leave off the () to see the
function: > db.getSiblingDB function (name) { return this.getMongo().getDB(name);
Cool functions ¨ printjson -> tojson ¨ forEach on array/query/cursor
> [{x:1},{y:1}].forEach(function(x){printjson(x)}) { "x" : 1 } { "y" : 1 } ¨ Object.bsonsize Object.bsonsize(c.findOne({name:”scott”})) ¨ load(file) ¨ run(file)
Print all Indexes db.getCollectionNames(). forEach(function(x){ print(“Collection: “ + x); printjson(db[x].getIndexes());
})
Getting the Biggest Doc var 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; } });
.mongorc.js ¨ File loaded at startup ¨ Can be used
to setup functions you always want defined ¨ Can set custom prompts
Questions ¨ That’s all…folks! ¨ try.mongodb.org – web based shell
¨
[email protected]