Common JavaScript statements
Copy Code code as follows:
Db.getsiblingdb (<dbname>)
Db.getcollectionnames ()
Db.getcollection (<collname>)
Db.printcollectionstats ()
Running JavaScript scripts on the MONGO shell
To switch a database:
Copy Code code as follows:
Run the following script:
var total = 0;
var dbastatcollections = function () {};
Dbastatcollections = function () {
collnames = Db.getcollectionnames ();
for (var index = 0; index < collnames.length; index++) {
var coll = db.getcollection (collnames[index));
var stats = coll.stats ();
Print (' ns,count,size,totalindexsize ');
Print (Stats.ns + ', ' + Stats.count + ', ' + stats.size + ', ' + stats.totalindexsize);
}
Dbastatcollections ();
The above script can be saved as dbastatcollections.js,
Run under the Linux shell
Copy Code code as follows:
MONGO Localhost:27017/<dbname> Dbastatcollections.js
Or run under the MONGO shell
Copy Code code as follows:
Load ("Dbastatcollections.js")
Store JavaScript functions on the server side
Db.system.js.remove ({"_id": "Dbastatcollections"});
Db.system.js.save (
{
_id: "Dbastatcollections",
value:function () {
collnames = Db.getcollectionnames ();
for (var index = 0; index < collnames.length; index++) {
var coll = db.getcollection (collnames[index));
var stats = coll.stats ();
Print (' ns,count,size,totalindexsize ');
Print (Stats.ns + ', ' + Stats.count + ', ' + stats.size + ', ' + Stats.totalindexsize);}}}
);
Db.loadserverscripts ();
Dbastatcollections ();
You can use this function in the current JavaScript context. When you exit the session, the function is not saved. Can only be performed at primary.
Note: The above output is saved as a CSV file open.
This article comes from the SQL Server Deep dives Blog