Here are few less used but important Mongodb queries I use now and then. Will keep updating this page If I get to use different less used queries.
Copy database
db.copyDatabase("dbname", "dbnamenew")
Drop database
use dbname
db.dropDatabase()
Active operations
db.currentOp() or db.adminCommand( { lockInfo: 1 } )
Kill operation
db.killOp(opid)
Query stats
db.collection.find({"status":"single"}).explain( "executionStats")
DB stats (in GB)
db.stats(1024*1024*1024)
Create index
db.collection.createIndex( { status: 1 } )
Get indexes on collection
db.collection.getIndexes()
Create read only access for user on mongodb
db.createUser({user: "example_read",pwd: "12345", roles: [{role:"read", db: "dbname"}]})
Get user roles on mongodb
db.getUser("user_name")
Export collection
mongoexport --host=10.0.0.1:27017 --username=example --authenticationDatabase=admin --collection=example_mapper --db=dbname --out=example_mapper.json
Import from collection
mongoimport --host=10.0.0.1:27017 --username=example --authenticationDatabase=admin --collection=example_mapper --db=dbname --file=example_mapper.json
Dump mongodb
mongodump --host=10.0.0.1:27017 --username=example --authenticationDatabase=admin --db db_example --out dumps/
Restore mongodb from dump
mongorestore --host=10.0.0.1:27017 --username=example --authenticationDatabase=admin dumps/
Shell connect with ssh
mongo --ssl --host docdb.amazonaws.com:27017 --sslCAFile rds.pem --username example --password asddasdasd
Top comments (0)