Commands - Mongodb
mongod --config "config.file"
mongod -f "config.file"
Status :
db.serverStatus();
db.help()
show dbs
use
db.cloneDatabase()
load() Executes a specified javascript file.
auth :
mongo admin -u root -p
use admin
db.auth(“AdminSTH”,”AdminSTH”)
list users :
show users
db.user.find()
db.user.find().pretty()
db.createUser({"user": "ajitesh", "pwd": "gurukul", "roles": ["readWrite", "dbAdmin"]})
db.user.drop()
show roles :
list collections:
show collections
db.collection.find()
db.printCollectionStats()
db.
db.
db.
db.
db.collection.drop()
User management commands :
db.createUser()
db.dropUser()
Collection management commands :
db.
db.
db.
Database management commands :
db.dropDatabase()
db.createCollection()
Database status command :
db.serverStatus()
Creating index with Database Command :
db.runCommand(
{ "createIndexes":
{ "indexes": [
{
"key": { "product": 1 }
},
{ "name": "name_index" }
]
}
)
Creating index with Shell Helper :
db.
{ "product": 1 },
{ "name": "name_index" }
)
Introspect a Shell Helper :
db.
Get the logging components:
mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
db.getLogComponents()
'
Change the logging level:
mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
db.setLogLevel(0, "index")
'
Tail the log file:
tail -f /data/db/mongod.log
Update a document:
mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
db.products.update( { "sku" : 6902667 }, { $set : { "salePrice" : 39.99} } )
'
Look for instructions in the log file with grep:
grep -R 'update' /data/db/mongod.log
rs.status()
Adding other members to replica set :
rs.add("m103.mongodb.university:27012")
rs.add("m103.mongodb.university:27013")
Getting an overview of the replica set topology :
rs.isMaster()
Stepping down the current primary :
rs.stepDown()
Checking replica set overview after election :
rs.isMaster()
Switch to config DB :
use config
Query config.databases:
db.databases.find().pretty()
Query config.collections:
db.collections.find().pretty()
Query config.shards:
db.shards.find().pretty()
Query config.chunks:
db.chunks.find().pretty()
Query config.mongos:
db.mongos.find().pretty()
Modifiy User multple Role :
var myuser = 'exemple';
var myre = /^exemple_dbs_.*/;
db.adminCommand('listDatabases')['databases'].forEach(
function(mydb) {
if (mydb['name'].match(myre)) {
db.grantRolesToUser(myuser, [ { role: "dbOwner", db: mydb['name'] } ] );
}
}
);
Modify a Role for Multiple Users
db.system.users.find({ "user": /^user_/ }).forEach(function(myuser) { db.grantRolesToUser(myuser["user"], [ { role: "dbOwner", db: "
Profiling slow query
for a db:
use db
db.setProfilingLevel(0, { slowms: 150 })
db.getProfilingStatus()
db.system.profile.find().pretty()
for a complete instance :
mongod --profile 1 --slowms 15 --slowOpSampleRate 0.5
2020-03-20 09:05:15
Comments
Add a Comment