Command Helpers


The mongo shell provides various help. The following table displays some common help methods and commands:

Help Methods and Commands Description
help Show help.
db.help() Show help for database methods.
db.<collection>.help() Show help on collection methods. The <collection> can be the name of an existing collection or a non-existing collection.
show dbs Print a list of all databases on the server.
use <db> Switch current database to <db>. The mongo shell variable db is set to the current database.

show collections

db.getCollectionNames()

Print a list of all collections for current database
show users Print a list of users for current database.
show roles Print a list of all roles, both user-defined and built-in, for the current database.
show profile Print the five most recent operations that took 1 millisecond or more. See documentation on the database profiler for more information.
show databases Print a list of all available databases.
load() Execute a JavaScript file. See Write Scripts for the mongo Shell for more information.

Backups

 

mongoimport -d mydb -c things --type csv --file locations.csv --headerline

locations.csv
Name,Address,City,State,ZIP
Jane Doe,123 Main St,Whereverville,CA,90210
John Doe,555 Broadway Ave,New York,NY,10010

 

/* query */
db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } );
{ "login": { $regex: 'богосло', $options: 'i' } }
{ "login": { $regex: /богосло/i } }

db.getCollection('links').find( { $where : "this.all !=  this.visited"  , vis_count:{$ne:7}} )

/* backup  */
mongodump --db gks --out /var/backups/mongobackups/`date +"%m-%d-%y"`
mongodump --db coord  --out C:\\
mongorestore --db minfin --drop c:\\minfin
mongorestore --db finance --drop c:\\finance
mongorestore --db coord --drop c:\\coord

	
/* robomongo  */
mongoimport --jsonArray -d coord -c penza --file c:\\rajons.json
db.loadServerScripts();

// авторизация  ================
use reporting
db.createUser({user:"admin",pwd:"123",roles:[{role:"readWrite",db:"accounts"}]})
db.getUsers()
use test
db.createUser( { user: "admin",
                 pwd: "123",
                 customData: { employeeId: 12345 },
                 roles: [ { role: "clusterAdmin", db: "admin" },
                          { role: "readAnyDatabase", db: "admin" },
                          "readWrite"] },
               { w: "majority" , wtimeout: 5000 } )
/* bat */
mongod --auth --port 27017 --dbpath C:\OpenServer\userdata\MongoDB-3.0-x64
// ================