MongoDB server-side JavaScript script
Common JavaScript statements
Db.getsiblingdb (<dbname>) db.getcollectionnames () db.getcollection (<collname>) Db.printcollectionstat S ()
Running JavaScript scripts in the MONGO shell
To switch databases:
Use <dbname>
Run the following script:
var total = 0;var dbastatcollections = function () {};d bastatcollections = 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,
Running under the Linux shell
MONGO Localhost:27017/<dbname> Dbastatcollections.js
Or run under the MONGO shell
Load ("Dbastatcollections.js")
Store JavaScript functions on the server
Db.system.js.remove ({"_id": "Dbastatcollections"});d B.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 + ', ' &NBSP;+&NBSP;STATS.Size + ', ' + stats.totalindexsize '); } });d b.loadserverscripts ();d bastatcollections ();
You can use this function in the current JavaScript context. After exiting the session, the function is not saved. Can only be performed in primary.
Note: The above output is saved as a CSV file open.
This article is from the SQL Server deep dives blog, so be sure to keep this source http://ultrasql.blog.51cto.com/9591438/1705153
MongoDB server-side JavaScript script