Mastering the MongoDB Javascript Shell

13,787 views

Published on

Published in: Technology
1 Comment
9 Likes
Statistics
Notes
  • ●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·.
    ONLY THREE DAYS TO VOTE IN THE CONTEST!.. VOTING ENDS NOV.29/10..
    ●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·. .
    Hello My Friend ♥
    ☀ We would very much appreciate if you would kindly take the time to view & vote for Doina’s two shows http://www.slideshare.net/doina/end-of-life-presentation-horror-story-contest-slideshare-2010 http://www.slideshare.net/doina/andante-slideshare-2010
    The shows are now on videos so you don’t have to download .. Thank you very much and enjoy a wonderful day ..
    Spiderwebb99 on Slideshare .. Vanessa ♥
    ●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·.☀•჻¨●.·☀•჻¨●·.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
13,787
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
86
Comments
1
Likes
9
Embeds 0
No embeds

No notes for slide

Mastering the MongoDB Javascript Shell

  1. 1. Mastering the Shell <br />Raw MongoDB (as you like it)<br />
  2. 2. What is the shell?<br />
  3. 3. What is it good for?<br />Debugging<br />Testing<br />Administration<br />Scripting Glue<br />Not 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<br />Everything is a 64bit fp (double)<br />Dates are a challenge<br />new Date(“1/1/1”)<br />Not Date(“1/1/1”) -> string<br />
  5. 5. The Bad: JS is Slow<br />Shell<br />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 />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 />ScottHernandez@gmail.com<br />

×