Hacking the Shell <br />Raw MongoDB (as you like it)<br />Scott Hernandez – scott@10gen<br />
What is the shell?<br />
What is it good for?<br />Debugging Queries/Syntax<br />Testing<br />Administration<br />Scripting Glue<br />Not for build...
The Bad: JS Types<br />Numbers Suck (but getting better)<br />32/64bit signed (int/long) – 1.5.4>NumberLong(“”)<br />Displ...
The Bad: JS is Slow<br />Shell<br />Always Safe/GLE<br />Loops and updates<br />Data conversions<br />Server<br />It prett...
Insert, Save, Update, Remove<br />Demo now <br />for(i = 0; i <1000; i++) { db.test.insert({x:i, ts: new Date()}) }<br />
Command Line<br />--eval<br />it/cursor<br />Printing values<br />Pass in a script<br />
Loading Scripts<br />Command line<br />load() – also runs<br />
Run Commands<br />db.runCommand({…})<br />db.runCommand(“getLastError”)<br />
Useful Commands<br />getCmdLineOpts<br />ping<br />isMaster<br />reIndex<br />sharding<br />replication<br />
Profiling<br />setProfilingLevel(lvl, <ms>)<br />0: none<br />1: time-based<br />2: all<br />getProfilingLevel()<br />Read...
Help<br />> help<br />> help admin<br />> help misc<br />> db.help()<br />> db.coll.help()<br />
Expose Functions<br />Leave off the () to see the function:<br />> db.getSisterDB<br />function (name) {<br />    return t...
Cool functions<br />printjson -> tojson<br />forEach on array/query/cursor<br />> [{x:1},{y:1}].forEach(function(x){printj...
Print all Indexes<br />db.getCollectionNames().<br />forEach(function(x){<br />	print(“Collection: “ + x);<br />printjson(...
Getting the Biggest Doc<br />var cursor = db.coll.find();<br />var biggest=0;<br />var doc = {};<br />cursor.forEach(funct...
Cursors: it<br />“it” global var<br />find automatically sets it<br />
Aliases<br />Show collections/tables<br />
DBRef<br />Has fetch()<br />Easy to create<br />
Questions<br />That’s all…folks!<br />scott@10gen.com<br />
Upcoming SlideShare
Loading in …5
×

MongoDB: Mastering the shell

1,891 views

Published on

Mastering the MongoDB JavaScript Shell

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,891
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

MongoDB: Mastering the shell

  1. 1. Hacking the Shell <br />Raw MongoDB (as you like it)<br />Scott Hernandez – scott@10gen<br />
  2. 2. What is the shell?<br />
  3. 3. What is it good for?<br />Debugging Queries/Syntax<br />Testing<br />Administration<br />Scripting Glue<br />Not for building apps, probably<br />
  4. 4. The Bad: JS Types<br />Numbers Suck (but getting better)<br />32/64bit signed (int/long) – 1.5.4>NumberLong(“”)<br />Displayed funny in early versions<br />Everything is a 64bit fp (double)<br />Dates are a challenge<br />new Date(“1/1/1”)<br />new ISODate(…)<br />Not Date(“1/1/1”) -> string<br />
  5. 5. The Bad: JS is Slow<br />Shell<br />Always Safe/GLE<br />Loops and updates<br />Data conversions<br />Server<br />It pretty much applies here too<br />Be careful with numbers as well<br />
  6. 6. Insert, Save, Update, Remove<br />Demo now <br />for(i = 0; i <1000; i++) { db.test.insert({x:i, ts: new Date()}) }<br />
  7. 7. Command Line<br />--eval<br />it/cursor<br />Printing values<br />Pass in a script<br />
  8. 8. Loading Scripts<br />Command line<br />load() – also runs<br />
  9. 9. Run Commands<br />db.runCommand({…})<br />db.runCommand(“getLastError”)<br />
  10. 10. Useful Commands<br />getCmdLineOpts<br />ping<br />isMaster<br />reIndex<br />sharding<br />replication<br />
  11. 11. Profiling<br />setProfilingLevel(lvl, <ms>)<br />0: none<br />1: time-based<br />2: all<br />getProfilingLevel()<br />Reading from profile collection<br />>db.system.profile.find()<br />
  12. 12. Help<br />> help<br />> help admin<br />> help misc<br />> db.help()<br />> db.coll.help()<br />
  13. 13. Expose Functions<br />Leave off the () to see the function:<br />> db.getSisterDB<br />function (name) {<br /> return this.getMongo().getDB(name);<br />
  14. 14. Cool functions<br />printjson -> tojson<br />forEach on array/query/cursor<br />> [{x:1},{y:1}].forEach(function(x){printjson(x)})<br />{ "x" : 1 }<br />{ "y" : 1 }<br />Object.bsonsize<br />Object.bsonsize(c.findOne({name:”scott”}))<br />load(file)<br />run(file)<br />
  15. 15. Print all Indexes<br />db.getCollectionNames().<br />forEach(function(x){<br /> print(“Collection: “ + x);<br />printjson(db[x].getIndexes());<br />})<br />
  16. 16. Getting the Biggest Doc<br />var cursor = db.coll.find();<br />var biggest=0;<br />var doc = {};<br />cursor.forEach(function (x) { <br />var size = Object.bsonsize(x); <br /> if (size > biggest) { biggest=size; doc = x; }<br />});<br />
  17. 17. Cursors: it<br />“it” global var<br />find automatically sets it<br />
  18. 18. Aliases<br />Show collections/tables<br />
  19. 19. DBRef<br />Has fetch()<br />Easy to create<br />
  20. 20. Questions<br />That’s all…folks!<br />scott@10gen.com<br />

×