Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use JavaScript script in MongoDB server

Shulou Source: shulou.com Published: 2022-05-31 21:11:28 09月19日 Update

This article is about how to use the JavaScript script in the MongoDB server, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

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

Var dbaStatCollections = function () {}

DbaStatCollections = function () {

CollNames = db.getCollectionNames ()

For (var index = 0; index < collNames.length; index++) {

Var coll = db.getCollection (collages [index])

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

The above is how to use the JavaScript script in the MongoDB server. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Scripts runs services functions more knowledge articles practical upper and lower context available in that is work meetings commonly used data databases articles see knowledge points industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker MySQL Apple MariaDB