Cmds¶
How can I check the size of a collection?¶
To view the size of a collection and other information, use the db.collection.stats() method from the mongo shell.
The following example issues db.collection.stats() for the orders collection:
db.orders.stats();
To view specific measures of size, use these methods:
db.collection.dataSize() # data size in bytes for the collection. db.collection.storageSize() # allocation size in bytes, including unused space. db.collection.totalSize() # the data size plus the index size in bytes. db.collection.totalIndexSize() # the index size in bytes.
Also, the following scripts print the statistics for each database and collection:
db._adminCommand("listDatabases").databases.forEach(function (d) {mdb = db.getSiblingDB(d.name); printjson(mdb.stats())}) db._adminCommand("listDatabases").databases.forEach(function (d) {mdb = db.getSiblingDB(d.name); mdb.getCollectionNames().forEach(function(c) {s = mdb[c].stats(); printjson(s)})})
How can I check the size of indexes?¶
To view the size of the data allocated for an index, use one of the following procedures in the mongo shell:
Use the db.collection.stats() method using the index namespace. To retrieve a list of namespaces, issue the following command:
db.system.namespaces.find()
Check the value of indexSizes in the output of the db.collection.stats() command.
Example: Issue the following command to retrieve index namespaces:
db.system.namespaces.find()
The command returns a list similar to the following:
{"name" : "test.orders"} {"name" : "test.system.indexes"} {"name" : "test.orders.$_id_"}
View the size of the data allocated for the orders.$_id_ index with the following sequence of operations:
use test db.orders.$_id_.stats().indexSizes
Connect to your remote MongoDB server¶
By using mongosh client:
$ sudo apt-get install -y mongodb-mongosh
Connect to db:
$ mongosh -u USERNAME -p PASSWORD IP/COLLECTION_NAME show dbs use My_DB_NAME show collections db.My_COLLECTIONS_NAME.find()
By using mongo client:
$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add - $ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list $ sudo apt-get update $ sudo apt-get install -y mongodb-org-shell
Connect to db:
$ mongo -u USERNAME -p PASSWORD IP/COLLECTION_NAME
Download mongosh cli¶
Download mongo cli
https://www.mongodb.com/try/download/shell
$ mongosh -u USERNAME -p PASSWORD IP/COLLECTION_NAME