【问题标题】:A list of indices in MongoDB?MongoDB中的索引列表?
【发布时间】:2011-02-16 21:45:19
【问题描述】:

有没有办法在 shell 中查看 mongodb 中集合的索引列表?我通读了http://www.mongodb.org/display/DOCS/Indexes,但我什么也没看到

【问题讨论】:

    标签: mongodb indexing database-indexes mongodb-indexes database


    【解决方案1】:

    从外壳:

    db.test.getIndexes()
    

    对于 shell 帮助,您应该尝试:

    help;
    db.help();
    db.test.help();
    

    【讨论】:

      【解决方案2】:

      确保您使用您的收藏:

      db.collection.getIndexes()
      

      http://docs.mongodb.org/manual/administration/indexes/#information-about-indexes

      【讨论】:

        【解决方案3】:

        如果您想获取数据库中所有索引的列表:

        use "yourdbname"
        
        db.system.indexes.find()
        

        【讨论】:

          【解决方案4】:

          您还可以输出所有索引及其大小:

          db.collectionName.stats().indexSizes
          

          还要检查 db.collectionName.stats() 是否为您提供了许多有趣的信息,例如 paddingFactor、集合的大小和其中的元素数量。

          【讨论】:

            【解决方案5】:

            如果要列出集合中的所有索引:

            db.getCollectionNames().forEach(function(collection) {
               indexes = db.getCollection(collection).getIndexes();
               print("Indexes for " + collection + ":");
               printjson(indexes);
            });
            

            【讨论】:

              【解决方案6】:

              更进一步,如果您想查找所有集合的所有索引,此脚本(根据 Juan Carlos Farah 的脚本 here 修改)为您提供一些有用的输出,包括索引详细信息的 JSON 打印输出:

               // Switch to admin database and get list of databases.
              db = db.getSiblingDB("admin");
              dbs = db.runCommand({ "listDatabases": 1}).databases;
              
              
              // Iterate through each database and get its collections.
              dbs.forEach(function(database) {
              db = db.getSiblingDB(database.name);
              cols = db.getCollectionNames();
              
              // Iterate through each collection.
              cols.forEach(function(col) {
              
                  //Find all indexes for each collection
                   indexes = db[col].getIndexes();
              
                   indexes.forEach(function(idx) {
                      print("Database:" + database.name + " | Collection:" +col+ " | Index:" + idx.name);
                      printjson(indexes);
                       });
              
              
                  });
              
              });
              

              【讨论】:

              • 这确实很有帮助,但我认为printjson(indexes);应该是printjson(idx);
              【解决方案7】:

              在此处使用旧版本的 MongoDB,但在此处使用 one of the top answers in this question 对我不起作用。这个有效:

              db.getCollectionNames().forEach(function(collection) {
                  print("Collection: '" + collection);
                  print(db.getCollection(collection).getIndexes())
              });
              

              【讨论】:

                猜你喜欢
                • 2023-03-06
                • 1970-01-01
                • 2016-05-23
                • 2012-08-23
                • 1970-01-01
                • 1970-01-01
                • 2021-07-04
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多