mongo - evanmoran/quick GitHub Wiki

Shell Commands

Information

show dbs                  // List databases on server
use <database>            // Change current database
show collections          // Show collections for current database
show users                // Show users for current database
show profile              // Show the last 5 db operations (more then 1ms)
show databases            // 2.4+ Show all available databases

db.<collection>           // Get database collection

Querying

db.<collection>.count()

// Get number of users
db.users.count()  

db.<collection>.find()

// Get all users
db.users.find()  

db.collection.find( <query> )

// Get all users with age greater then or equal to 18 less then 64
db.users.find( {age: { $gte: 18, $lt: 64 }} );  

db.collection.find( <query>, <projection> )

db.users.find ( 
  { },
  { name: true }
);

db.<collection>.find(<query>).sort( <sort order> );

// Find all users sorted in ascending order by name
// 1 means acending, -1 means decending
db.users.find().sort( { name: 1 } );  

db.<collection>.find(<query>).limit(<count>)

db.users.find().limit(5); 

db.<collection>.find(<query>).skip(<count>)

// Find 5 users after the first 5
db.users.find().skip(5).limit(5); 

db.<collection>.findOne( <query> )

// Find 1 user between the ages 18 and 64
db.users.findOne( {age: { $gte: 18, $lt: 64 }} );

Prompts:

// db@host
var host = db.serverStatus().host;
var prompt = function() { return db+"@"+host+"\> "; }

More Info

⚠️ **GitHub.com Fallback** ⚠️