Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript script in MongoDB server

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report