MongoDB server-side JavaScript script

Source: Internet
Author: User
Tags mongodb server mongo shell

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.