20
MASTERING THE SHELL Raw MongoDB (as you like it)

Mastering the MongoDB Javascript Shell

Embed Size (px)

Citation preview

Page 1: Mastering the MongoDB Javascript Shell

MASTERING THE SHELL

Raw MongoDB (as you like it)

Page 2: Mastering the MongoDB Javascript Shell

What is the shell?

•vars•funct

ions•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

Page 3: Mastering the MongoDB Javascript Shell

What is it good for?

Debugging Testing Administration Scripting Glue Not building apps, probably

Page 4: Mastering the MongoDB Javascript Shell

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

Page 5: Mastering the MongoDB Javascript Shell

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

Page 6: Mastering the MongoDB Javascript Shell

Insert, Save, Update, Remove Demo now

for(i = 0; i <1000; i++) { db.test.insert({x:i, ts: new Date()}) }

Page 7: Mastering the MongoDB Javascript Shell

Command Line

--eval it/cursor Printing values

Pass in a script

Page 8: Mastering the MongoDB Javascript Shell

Loading Scripts

Command line load() – also runs

Page 9: Mastering the MongoDB Javascript Shell

Run Commands

db.runCommand({…})

db.runCommand(“getLastError”)

Page 10: Mastering the MongoDB Javascript Shell

Useful Commands

getCmdLineOpts ping isMaster reIndex sharding replication

Page 11: Mastering the MongoDB Javascript Shell

Profiling

setProfilingLevel(lvl, <ms>) 0: none 1: time-based 2: all

Reading from profile collection>db.system.profile.find()

Page 12: Mastering the MongoDB Javascript Shell

Help

> help> help admin> help misc> db.help()> db.coll.help()

Page 13: Mastering the MongoDB Javascript Shell

Expose Functions

Leave off the () to see the function:

> db.getSisterDBfunction (name) { return this.getMongo().getDB(name);

Page 14: Mastering the MongoDB Javascript Shell

Cool functions

printjson -> tojson forEach on array/query/cursor> [{x:1},{y:1}].forEach(function(x){printjson(x)})

{ "x" : 1 }

{ "y" : 1 }

Object.bsonsizeObject.bsonsize(c.findOne({name:”scott”}))

load(file) run(file)

Page 15: Mastering the MongoDB Javascript Shell

Print all Indexes

db.getCollectionNames().forEach(function(x){

print(“Collection: “ + x);printjson(db[x].getIndexes());

})

Page 16: Mastering the MongoDB Javascript Shell

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; }

});

Page 17: Mastering the MongoDB Javascript Shell

Cursors: it

“it” global var find automatically sets it

Page 18: Mastering the MongoDB Javascript Shell

Aliases

Show collections/tables

Page 19: Mastering the MongoDB Javascript Shell

DBRef

Has fetch() Easy to create

Page 20: Mastering the MongoDB Javascript Shell

Questions

That’s all…folks!

[email protected]