SlideShare ist ein Scribd-Unternehmen logo
1 von 25
#MongoDBDays




MongoDB Shell Tips &
Tricks
Hannes Magnusson
Driver Engineer, 10gen
@bjori
What is the Shell?
  Embedded
                    • vars / functions / data structs + types
  Javascript        • Spidermonkey / V8
  Interpreter
                    • ObjectId("...")
Global Functions    • new Date()
  and Objects       • Object.bsonsize()


MongoDB driver      • db["collection"].find/count/update
   Exposed          • short-hand for collections



                    • Doesn't require quoted keys
JSON-like stuff     • Don’t copy and paste too much


                   Shell Tips & Tricks, Hannes Magnusson
MongoDB Shell: Advantages
• Your goto tool
• Debugging Queries / Syntax
• Testing
• Administration
• Scripting Glue
• You don’t do rs.stepDown() from your application



                   Shell Tips & Tricks, Hannes Magnusson
MongoDB Shell: Disadvantages
• Numbers in JS are a pain
  – 32/64-bit int/long  NumberInt() / NumberLong()
  – Primitive numbers are all 64-bit FP doubles

• Dates can be confusing
  – new Date( "1/1/1" )
  – new ISODate( ... )

          NOT: Date( "1/1/1" )  string




                    Shell Tips & Tricks, Hannes Magnusson
Emacs-like shell bindings
 ctrl A   Move cursor to start of line
 ctrl E   Move cursor to end of line
 meta B   Move cursor left by one word
 meta F   Move cursor right by one word
 ctrl L   Clear screen and redisplay line
 ctrl R   Reverse history search
 meta <   Start of history
 meta >   End of history


              Shell Tips & Tricks, Hannes Magnusson
Easy to use
• Tab completion on most objects
• Built-in help on most objects (.help())
• show
   –   profile # 5 most recent ops of 1ms or more
   –   users # List all the users of the current db
   –   dbs # List all the databases
   –   logs # List all available logs
• Most examples use the shell


                       Shell Tips & Tricks, Hannes Magnusson
Speed Considerations
• Shell
   – JavaScript is slow
   – Always in "write-acknowledged" (safe mode) / GLE
   – Data Conversions

• Server
   – Applies on the server as well
   – Careful with round-tripping numbers




                    Shell Tips & Tricks, Hannes Magnusson
Insert, Update, Remove

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


                Shell Tips & Tricks, Hannes Magnusson
Loading Scripts
• Commandline
   – --eval switch
   – .js files

• Within the shell
   – load()




                     Shell Tips & Tricks, Hannes Magnusson
Running Commands
• db.runCommand( { ... } )
   – Runs any arbitrary command against the current DB

• db.adminCommand( { ... } )
   – Run commands against the admin database




                    Shell Tips & Tricks, Hannes Magnusson
db.adminCommand
Definition
function (obj) {
  if (this._name == "admin") {
      return this.runCommand(obj);
  }
  return this.getSiblingDB("admin")
        .runCommand(obj);
}

             Shell Tips & Tricks, Hannes Magnusson
Profiling
• setProfilingLevel( lvl, <ms> )
   – 0: none
   – 1: time-based
   – 2: all

• getProfilingLevel()
• Reading from the profile collection
   – db.system.profile.find()




                      Shell Tips & Tricks, Hannes Magnusson
Cool Functions
• printjson()  tojson
• forEach on arrays, queries, and cursors




                  Shell Tips & Tricks, Hannes Magnusson
You didn’t hear it from me

db.system.js.save({ _id:"echoFunction",
     value : function(x) { return x; }
})
db.loadServerScripts();
echoFunction(3);


              Shell Tips & Tricks, Hannes Magnusson
forEach example

[{x:1},{y:1}].forEach(function(x) {
   printjson(x)
})

{ "x" : 1 }
{ "y" : 1 }



              Shell Tips & Tricks, Hannes Magnusson
Cool Functions
• printjson  tojson
• forEach on arrays, queries, and cursors
• Object.bsonsize
• load(file)
• run(file)




                 Shell Tips & Tricks, Hannes Magnusson
Print all Indexes

db.getCollectionNames().
forEach( function(x) {
   print( "Collection: " + x );
   printjson( db[x].getIndexes() );
})




              Shell Tips & Tricks, Hannes Magnusson
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;
  }
});
                Shell Tips & Tricks, Hannes Magnusson
Administration functions
• Sharding
   – sh.status()
   – sh.enableSharding(dbname)

• Replicaset
   – rs.status()
   – cfg = rs.config(); rs.reconfig(cfg);
   – db.isMaster()

• Status
   – Object.bsonsize(document)
   – db.collectionName.stats()

                       Shell Tips & Tricks, Hannes Magnusson
.mongorc.js
• Automagically loaded on startup
   – Unless you specify --norc

• Script your command prompt
   – prompt=function() { return "Hello World"; }

• Colorize output
• Move complex aggregate() queries into functions
• Can be a directory



                     Shell Tips & Tricks, Hannes Magnusson
Want to know more?
         The shell is self documented, in JavaScript o/
                            …Except the native helper

• help
   – dbs # Shows all commands you can run on a database
   – connect # Connect to other nodes

• Call a function, without the brackets
   – Will show you the actual code behind it


• try.mongodb.org !


                     Shell Tips & Tricks, Hannes Magnusson
#MongoDBDays




Thank You
Hannes Magnusson
Driver Engineer, 10gen
@bjori

Weitere ähnliche Inhalte

Was ist angesagt?

Dirty - How simple is your database?
Dirty - How simple is your database?Dirty - How simple is your database?
Dirty - How simple is your database?Felix Geisendörfer
 
Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?Felix Geisendörfer
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)Felix Geisendörfer
 
Large Scale Log collection using LogStash & mongoDB
Large Scale Log collection using LogStash & mongoDB Large Scale Log collection using LogStash & mongoDB
Large Scale Log collection using LogStash & mongoDB Gaurav Bhardwaj
 
Debugging and Testing ES Systems
Debugging and Testing ES SystemsDebugging and Testing ES Systems
Debugging and Testing ES SystemsChris Birchall
 
To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)Geoffrey Anderson
 
How to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DB
How to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DBHow to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DB
How to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DBMicrosoft Tech Community
 
CPAN 模組二三事
CPAN 模組二三事CPAN 模組二三事
CPAN 模組二三事Lin Yo-An
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
Mongo db in 3 minutes BoilerMake
Mongo db in 3 minutes   BoilerMakeMongo db in 3 minutes   BoilerMake
Mongo db in 3 minutes BoilerMakeValeri Karpov
 
Introduction to Node.js: perspectives from a Drupal dev
Introduction to Node.js: perspectives from a Drupal devIntroduction to Node.js: perspectives from a Drupal dev
Introduction to Node.js: perspectives from a Drupal devmcantelon
 

Was ist angesagt? (20)

Node.js - A Quick Tour II
Node.js - A Quick Tour IINode.js - A Quick Tour II
Node.js - A Quick Tour II
 
Dirty - How simple is your database?
Dirty - How simple is your database?Dirty - How simple is your database?
Dirty - How simple is your database?
 
Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?
 
Node.js - As a networking tool
Node.js - As a networking toolNode.js - As a networking tool
Node.js - As a networking tool
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)
 
Nodejs - A quick tour (v5)
Nodejs - A quick tour (v5)Nodejs - A quick tour (v5)
Nodejs - A quick tour (v5)
 
Node.js
Node.jsNode.js
Node.js
 
Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)
 
Large Scale Log collection using LogStash & mongoDB
Large Scale Log collection using LogStash & mongoDB Large Scale Log collection using LogStash & mongoDB
Large Scale Log collection using LogStash & mongoDB
 
Aurinko
AurinkoAurinko
Aurinko
 
Node.js - A Quick Tour
Node.js - A Quick TourNode.js - A Quick Tour
Node.js - A Quick Tour
 
Debugging and Testing ES Systems
Debugging and Testing ES SystemsDebugging and Testing ES Systems
Debugging and Testing ES Systems
 
To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)
 
Fluent plugin-dstat
Fluent plugin-dstatFluent plugin-dstat
Fluent plugin-dstat
 
ElasticSearch
ElasticSearchElasticSearch
ElasticSearch
 
How to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DB
How to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DBHow to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DB
How to migrate your existing MongoDB and Cassandra Apps to Azure Cosmos DB
 
CPAN 模組二三事
CPAN 模組二三事CPAN 模組二三事
CPAN 模組二三事
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
Mongo db in 3 minutes BoilerMake
Mongo db in 3 minutes   BoilerMakeMongo db in 3 minutes   BoilerMake
Mongo db in 3 minutes BoilerMake
 
Introduction to Node.js: perspectives from a Drupal dev
Introduction to Node.js: perspectives from a Drupal devIntroduction to Node.js: perspectives from a Drupal dev
Introduction to Node.js: perspectives from a Drupal dev
 

Ähnlich wie Shell Tips and Tricks

MongoDB at the Silicon Valley iPhone and iPad Developers' Meetup
MongoDB at the Silicon Valley iPhone and iPad Developers' MeetupMongoDB at the Silicon Valley iPhone and iPad Developers' Meetup
MongoDB at the Silicon Valley iPhone and iPad Developers' MeetupMongoDB
 
Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4MongoDB
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbMongoDB APAC
 
Mongodb intro
Mongodb introMongodb intro
Mongodb introchristkv
 
MongoDB at ZPUGDC
MongoDB at ZPUGDCMongoDB at ZPUGDC
MongoDB at ZPUGDCMike Dirolf
 
MongoDB at Scale
MongoDB at ScaleMongoDB at Scale
MongoDB at ScaleMongoDB
 
London MongoDB User Group April 2011
London MongoDB User Group April 2011London MongoDB User Group April 2011
London MongoDB User Group April 2011Rainforest QA
 
Ops Jumpstart: MongoDB Administration 101
Ops Jumpstart: MongoDB Administration 101Ops Jumpstart: MongoDB Administration 101
Ops Jumpstart: MongoDB Administration 101MongoDB
 
MongoDB at FrozenRails
MongoDB at FrozenRailsMongoDB at FrozenRails
MongoDB at FrozenRailsMike Dirolf
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewAntonio Pintus
 
MongoDB Command Line Tools
MongoDB Command Line ToolsMongoDB Command Line Tools
MongoDB Command Line ToolsRainforest QA
 
Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26kreuter
 
Scaling MongoDB
Scaling MongoDBScaling MongoDB
Scaling MongoDBMongoDB
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!Daniel Cousineau
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring ClojurescriptLuke Donnet
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsMike North
 
The Care + Feeding of a Mongodb Cluster
The Care + Feeding of a Mongodb ClusterThe Care + Feeding of a Mongodb Cluster
The Care + Feeding of a Mongodb ClusterChris Henry
 
Mongo Berlin - Mastering the Shell
Mongo Berlin - Mastering the ShellMongo Berlin - Mastering the Shell
Mongo Berlin - Mastering the ShellMongoDB
 
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB ShellMongoDB
 

Ähnlich wie Shell Tips and Tricks (20)

MongoDB at the Silicon Valley iPhone and iPad Developers' Meetup
MongoDB at the Silicon Valley iPhone and iPad Developers' MeetupMongoDB at the Silicon Valley iPhone and iPad Developers' Meetup
MongoDB at the Silicon Valley iPhone and iPad Developers' Meetup
 
Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4Webinar: Was ist neu in MongoDB 2.4
Webinar: Was ist neu in MongoDB 2.4
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
Buildingsocialanalyticstoolwithmongodb
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
MongoDB at ZPUGDC
MongoDB at ZPUGDCMongoDB at ZPUGDC
MongoDB at ZPUGDC
 
MongoDB at Scale
MongoDB at ScaleMongoDB at Scale
MongoDB at Scale
 
London MongoDB User Group April 2011
London MongoDB User Group April 2011London MongoDB User Group April 2011
London MongoDB User Group April 2011
 
Ops Jumpstart: MongoDB Administration 101
Ops Jumpstart: MongoDB Administration 101Ops Jumpstart: MongoDB Administration 101
Ops Jumpstart: MongoDB Administration 101
 
MongoDB at FrozenRails
MongoDB at FrozenRailsMongoDB at FrozenRails
MongoDB at FrozenRails
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
 
MongoDB Command Line Tools
MongoDB Command Line ToolsMongoDB Command Line Tools
MongoDB Command Line Tools
 
MongoDB 3.0
MongoDB 3.0 MongoDB 3.0
MongoDB 3.0
 
Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26
 
Scaling MongoDB
Scaling MongoDBScaling MongoDB
Scaling MongoDB
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.js
 
The Care + Feeding of a Mongodb Cluster
The Care + Feeding of a Mongodb ClusterThe Care + Feeding of a Mongodb Cluster
The Care + Feeding of a Mongodb Cluster
 
Mongo Berlin - Mastering the Shell
Mongo Berlin - Mastering the ShellMongo Berlin - Mastering the Shell
Mongo Berlin - Mastering the Shell
 
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
 

Mehr von MongoDB

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump StartMongoDB
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB
 

Mehr von MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Shell Tips and Tricks

  • 1. #MongoDBDays MongoDB Shell Tips & Tricks Hannes Magnusson Driver Engineer, 10gen @bjori
  • 2. What is the Shell? Embedded • vars / functions / data structs + types Javascript • Spidermonkey / V8 Interpreter • ObjectId("...") Global Functions • new Date() and Objects • Object.bsonsize() MongoDB driver • db["collection"].find/count/update Exposed • short-hand for collections • Doesn't require quoted keys JSON-like stuff • Don’t copy and paste too much Shell Tips & Tricks, Hannes Magnusson
  • 3. MongoDB Shell: Advantages • Your goto tool • Debugging Queries / Syntax • Testing • Administration • Scripting Glue • You don’t do rs.stepDown() from your application Shell Tips & Tricks, Hannes Magnusson
  • 4. MongoDB Shell: Disadvantages • Numbers in JS are a pain – 32/64-bit int/long  NumberInt() / NumberLong() – Primitive numbers are all 64-bit FP doubles • Dates can be confusing – new Date( "1/1/1" ) – new ISODate( ... ) NOT: Date( "1/1/1" )  string Shell Tips & Tricks, Hannes Magnusson
  • 5. Emacs-like shell bindings ctrl A Move cursor to start of line ctrl E Move cursor to end of line meta B Move cursor left by one word meta F Move cursor right by one word ctrl L Clear screen and redisplay line ctrl R Reverse history search meta < Start of history meta > End of history Shell Tips & Tricks, Hannes Magnusson
  • 6.
  • 7. Easy to use • Tab completion on most objects • Built-in help on most objects (.help()) • show – profile # 5 most recent ops of 1ms or more – users # List all the users of the current db – dbs # List all the databases – logs # List all available logs • Most examples use the shell Shell Tips & Tricks, Hannes Magnusson
  • 8. Speed Considerations • Shell – JavaScript is slow – Always in "write-acknowledged" (safe mode) / GLE – Data Conversions • Server – Applies on the server as well – Careful with round-tripping numbers Shell Tips & Tricks, Hannes Magnusson
  • 9. Insert, Update, Remove for ( i = 0; i < 1000; i++ ) { db.test.insert( { x: i, ts: new Date() } ); } Shell Tips & Tricks, Hannes Magnusson
  • 10. Loading Scripts • Commandline – --eval switch – .js files • Within the shell – load() Shell Tips & Tricks, Hannes Magnusson
  • 11. Running Commands • db.runCommand( { ... } ) – Runs any arbitrary command against the current DB • db.adminCommand( { ... } ) – Run commands against the admin database Shell Tips & Tricks, Hannes Magnusson
  • 12. db.adminCommand Definition function (obj) { if (this._name == "admin") { return this.runCommand(obj); } return this.getSiblingDB("admin") .runCommand(obj); } Shell Tips & Tricks, Hannes Magnusson
  • 13. Profiling • setProfilingLevel( lvl, <ms> ) – 0: none – 1: time-based – 2: all • getProfilingLevel() • Reading from the profile collection – db.system.profile.find() Shell Tips & Tricks, Hannes Magnusson
  • 14. Cool Functions • printjson()  tojson • forEach on arrays, queries, and cursors Shell Tips & Tricks, Hannes Magnusson
  • 15. You didn’t hear it from me db.system.js.save({ _id:"echoFunction", value : function(x) { return x; } }) db.loadServerScripts(); echoFunction(3); Shell Tips & Tricks, Hannes Magnusson
  • 16.
  • 17. forEach example [{x:1},{y:1}].forEach(function(x) { printjson(x) }) { "x" : 1 } { "y" : 1 } Shell Tips & Tricks, Hannes Magnusson
  • 18. Cool Functions • printjson  tojson • forEach on arrays, queries, and cursors • Object.bsonsize • load(file) • run(file) Shell Tips & Tricks, Hannes Magnusson
  • 19. Print all Indexes db.getCollectionNames(). forEach( function(x) { print( "Collection: " + x ); printjson( db[x].getIndexes() ); }) Shell Tips & Tricks, Hannes Magnusson
  • 20. 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; } }); Shell Tips & Tricks, Hannes Magnusson
  • 21. Administration functions • Sharding – sh.status() – sh.enableSharding(dbname) • Replicaset – rs.status() – cfg = rs.config(); rs.reconfig(cfg); – db.isMaster() • Status – Object.bsonsize(document) – db.collectionName.stats() Shell Tips & Tricks, Hannes Magnusson
  • 22.
  • 23. .mongorc.js • Automagically loaded on startup – Unless you specify --norc • Script your command prompt – prompt=function() { return "Hello World"; } • Colorize output • Move complex aggregate() queries into functions • Can be a directory Shell Tips & Tricks, Hannes Magnusson
  • 24. Want to know more? The shell is self documented, in JavaScript o/ …Except the native helper • help – dbs # Shows all commands you can run on a database – connect # Connect to other nodes • Call a function, without the brackets – Will show you the actual code behind it • try.mongodb.org ! Shell Tips & Tricks, Hannes Magnusson

Hinweis der Redaktion

  1. http://try.mongodb.org/– it is almost a full clone of the shellThe shell is your buddy, your goto tool for quick hacking/tasksIt is important to know and bridges everyone using MongoDB as it doesn’t include any of the language specific cruft or dependencies– just raw MongoDB :D !
  2. Doesn’t matter which language you use, everyone else knows the shellAll examples during this conference are based on the shell
  3. ----- Meeting Notes (1/16/13 17:30) -----anecdote
  4. Unfortunately, we hire a lot of Brown students who apparently learn emacs :(No vi bindings… YET!
  5. Believe it or not, show tables works too :PIf you care what is behind it, serverBuildInfo.interpreterVersionIt’s the common ground between the languages
  6. Show Demo #1
  7. Show Demo #2
  8. See implementation by omitting parens()
  9. do demo #3
  10. do demo #4