After going through CRUD operations on MongoDB database, We
will take look at some more basic MongoDB Administration
Getting
MongoDB database-related information
If
you want to know databases available in the system, execute "show dbs" command, it will
list out all the databases
>
show dbs;
admin
0.000GB
config
0.000GB
local
0.016GB
techno_db
0.001GB
>
If
you want to know in which database, you are currently execute db
>
db
techno_db
>
if
you are curious about what are the collections available under the database, just
execute show collections or show tables
>show collections;
tech_large
techno_col
>
or
>
show tables;
tech_large
techno_col
>
The
command db.stats() will give you all the stats about the database
>
db.stats();
{
"db" : "techno_db",
"collections" : 2,
"views" : 0,
"objects" : 17000,
"avgObjSize" : 37.64705882352941,
"dataSize" : 640000,
"storageSize" : 335872,
"numExtents" : 0,
"indexes" : 2,
"indexSize" : 208896,
"fsUsedSize" : 587198464,
"fsTotalSize" : 10693378048,
"ok" : 1,
"operationTime" : Timestamp(1549877990, 1),
"$clusterTime" : {
"clusterTime" :
Timestamp(1549877990, 1),
"signature" : {
"hash"
: BinData(0,"OFOjmV2iLyqywB6fMf4bdiUZ5CU="),
"keyId" : NumberLong("6636938165653340162")
}
}
}
>
If
you want to know stats about only particular collections then that is also
possible, execute db..stats() command
>
db.techno_col.stats();
{
"ns" : "techno_db.techno_col",
"size" : 70000,
"count" : 2000,
"avgObjSize" : 35,
"storageSize" : 86016,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
},
"creationString" :
…
…
…
},
"nindexes" : 1,
"totalIndexSize" : 61440,
"indexSizes" : {
"_id_" : 61440
},
"ok" : 1,
"operationTime" : Timestamp(1549878100, 1),
"$clusterTime" : {
"clusterTime" :
Timestamp(1549878100, 1),
"signature" : {
"hash" : BinData(0,"eBwJSS4K5vUHyZJKT54h2pduSEc="),
"keyId" : NumberLong("6636938165653340162")
}
}
}
>
Getting
Help
MongoDB
provides by default build command called db.help(), it lists out commonly used methods for operating database
we
can get the type command on any collections using
db..help()
>
db.help();
DB
methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db,
and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a
collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) - deprecated
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - deprecated
db.createCollection(name, {size: ..., capped: ..., max:
...})
db.createView(name, viewOn, [{$operator: {...}}, ...],
{viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in
the db
...
...
...
db.shutdownServer()
db.stats()
db.version() current version of the server
>
>
db.techno_col.help();
DBCollection
help
db.techno_col.find().help() - show DBCursor help
db.techno_col.bulkWrite( operations, )- ulk execute write operations, optional parameters are: w,
wtimeout, j
db.techno_col.count( query = {}, )
- count the number of documents that matches the query, optional parameters
are: limit, skip, hint, maxTimeMS
...
...
...
db.techno_col.unsetWriteConcern(
) - unsets the write concern for writes to the collection
db.techno_col.latencyStats() - display operation latency
histograms for this collection
>
There
is one method to increase the speed of your administration by simply using the tab key
on the keyboard
Just
type function or procedure name and press tab, it will all available functions
under it
> db.techno_col.get
db.techno_col.getCollection( db.techno_col.getIndexSpecs( db.techno_col.getName( db.techno_col.getShardVersion(
db.techno_col.getDB( db.techno_col.getIndexes( db.techno_col.getPlanCache( db.techno_col.getSlaveOk(
db.techno_col.getFullName( db.techno_col.getIndices( db.techno_col.getQueryOptions( db.techno_col.getSplitKeysForChunks(
db.techno_col.getIndexKeys(
db.techno_col.getMongo( db.techno_col.getShardDistribution(
db.techno_col.getWriteConcern(
To
get the definition of any method can be found using not providing brackets at
the time of invocation of the method
for
example
>
db.techno_col.getIndexes
function
(filter) {
var res = this._getIndexesCommand(filter);
if (res) {
return res;
}
return this._getIndexesSystemIndexes(filter);
}
>