MongoDB: Mastering the shell
Upcoming SlideShare
Loading in...5
×
 

MongoDB: Mastering the shell

on

  • 1,904 views

Mastering the MongoDB JavaScript Shell

Mastering the MongoDB JavaScript Shell

Statistics

Views

Total Views
1,904
Views on SlideShare
1,904
Embed Views
0

Actions

Likes
0
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

MongoDB: Mastering the shell MongoDB: Mastering the shell Presentation Transcript

  • Hacking the Shell
    Raw MongoDB (as you like it)
    Scott Hernandez – scott@10gen
  • What is the shell?
  • What is it good for?
    Debugging Queries/Syntax
    Testing
    Administration
    Scripting Glue
    Not for building apps, probably
  • The Bad: JS Types
    Numbers Suck (but getting better)
    32/64bit signed (int/long) – 1.5.4>NumberLong(“”)
    Displayed funny in early versions
    Everything is a 64bit fp (double)
    Dates are a challenge
    new Date(“1/1/1”)
    new ISODate(…)
    Not Date(“1/1/1”) -> string
  • The Bad: JS is Slow
    Shell
    Always Safe/GLE
    Loops and updates
    Data conversions
    Server
    It pretty much applies here too
    Be careful with numbers as well
  • Insert, Save, Update, Remove
    Demo now 
    for(i = 0; i <1000; i++) { db.test.insert({x:i, ts: new Date()}) }
  • Command Line
    --eval
    it/cursor
    Printing values
    Pass in a script
  • Loading Scripts
    Command line
    load() – also runs
  • Run Commands
    db.runCommand({…})
    db.runCommand(“getLastError”)
  • Useful Commands
    getCmdLineOpts
    ping
    isMaster
    reIndex
    sharding
    replication
  • Profiling
    setProfilingLevel(lvl, <ms>)
    0: none
    1: time-based
    2: all
    getProfilingLevel()
    Reading from profile collection
    >db.system.profile.find()
  • Help
    > help
    > help admin
    > help misc
    > db.help()
    > db.coll.help()
  • Expose Functions
    Leave off the () to see the function:
    > db.getSisterDB
    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; }
    });
  • Cursors: it
    “it” global var
    find automatically sets it
  • Aliases
    Show collections/tables
  • DBRef
    Has fetch()
    Easy to create
  • Questions
    That’s all…folks!
    scott@10gen.com