简体   繁体   English

如何获取 MongoDB 所有索引的使用情况和大小

[英]How to get MongoDB all indexes usage and size

I am trying to get MongoDB indexes usage and size in the same output.我正在尝试在同一个 output 中获取 MongoDB 索引使用情况和大小。

db.getCollection(collection).stats().indexSizes; gives sizes给出尺寸

and db.getCollection(collection).aggregate({ "$indexStats": {} });db.getCollection(collection).aggregate({ "$indexStats": {} }); gives just usage.只给出用法。

Is there a command to output both?是否有针对 output 的命令?

Both queries can be combined on intellishell, since it uses syntax similar to javascript we can run the following script.这两个查询可以在 intellishell 上组合,因为它使用类似于 javascript 的语法,我们可以运行以下脚本。

db = db.getSiblingDB("your-db-name");
const collections = db.getCollectionNames();

const result = [];
for (const collection of collections) {
    const sizes = db.getCollection(collection).stats().indexSizes;
    const indexStats = db.getCollection(collection).aggregate({ "$indexStats": {} }).toArray();
    for (const index of indexStats) {
        index.collection = collection;
        index.size = sizes[index.name];
        result.push(index)
    }
}

print (result)

notice the conversion to .toArray() in order to loop easily over it.注意转换为.toArray()以便轻松循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM