Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB server JavaScript script

Shulou Source: shulou.com Published: 2022-06-01 15:50:16 11月02日 Update

MongoDB server JavaScript script

Common JavaScript statements

Db.getSiblingDB () db.getCollectionNames () db.getCollection () db.printCollectionStats ()

Run the JavaScript script on mongo shell

Switch the database:

Use

Run the following script:

Var total = 0 index dbaStatCollections = function () {}; dbaStatCollections = function () {collNames = db.getCollectionNames (); for (var index = 0; index < index) {var coll = db.getCollection; var stats = coll.stats (); print ('ns,count,size,totalIndexSize'); print (stats.ns +','+ stats.count +','+ stats.size +','+ stats.totalIndexSize) }} dbaStatCollections ()

You can save the above script as dbaStatCollections.js

Run under linux shell

Mongo localhost:27017/ dbaStatCollections.js

Or run under mongo shell

Load ("dbaStatCollections.js")

Store JavaScript functions on the server

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); 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. After exiting the session, the function will not be saved. Can only be executed in Primary.

Note: the above output is saved as a CSV file to open.

Store the JavaScript function in the mongo shell startup configuration file .mongorc.js

Mongodb shell loads the ~ / .mongorc.js file by default, and you can customize the prompt variable to modify the shell prompt. You can also put the above commonly used DBA management functions here.

Tags: Functions scripts runs files services commonly used storage up and down context information variables available in comments data databases results statements can be toggle prompt Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Microsoft macOS Redmi