<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # MongoDB 索引教程 > 原文: [https://beginnersbook.com/2017/09/mongodb-indexing-tutorial-with-example/](https://beginnersbook.com/2017/09/mongodb-indexing-tutorial-with-example/) MongoDB 中的**索引**是一種特殊的數據結構,它保存創建索引的少量文檔字段的數據。索引提高了數據庫中搜索操作的速度,因為不是搜索整個文檔,而是對僅包含少量字段的索引執行搜索。另一方面,由于索引使用了額外的寫入和額外數據空間,索引太多會妨礙插入,更新和刪除操作的性能。 ## 如何在 MongoDB 中創建索引 **語法:** ```js db.collection_name.createIndex({field_name: 1 or -1}) ``` 值 1 表示升序,-1 表示降序。 例如,我有一個集合`studentdata`。此集合中的文檔包含以下字段:`student_name`,`student_id`和`student_age` 假設我想按升序在`student_name`字段上創建索引: ```js db.studentdata.createIndex({student_name: 1}) ``` **輸出:** ```js { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } ``` ![MongoDB create index](https://img.kancloud.cn/1e/d9/1ed9836396e9093a76ec75ea40931bdb_512x244.jpg) 我們在`student_name`上創建了索引,這意味著當有人根據`student_name`搜索文檔時,搜索速度會更快,因為索引將用于此搜索。因此,在字段中創建將在集合中頻繁搜索的索引非常重要。 ## MongoDB - 查找集合中的索引 我們可以使用`getIndexes()`方法來查找在集合上創建的所有索引。此方法的語法是: ```js db.collection_name.getIndexes() ``` 因此,要獲取`studentdata`集合的索引,命令將是: ```js > db.studentdata.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.studentdata" }, { "v" : 2, "key" : { "student_name" : 1 }, "name" : "student_name_1", "ns" : "test.studentdata" } ] ``` 輸出顯示我們在此集合中有兩個索引。在`_id`上創建的默認索引以及我們在`student_name`字段上創建的索引。 ## MongoDB - 刪除集合中的索引 您可以刪除特定索引或所有索引。 **刪除特定索引:** 為此,使用了`dropIndex()`方法。 ```js db.collection_name.dropIndex({index_name: 1}) ``` 讓我們刪除我們在集合`studentdata`中的`student_name`字段上創建的索引。這個命令: ```js db.studentdata.dropIndex({student_name: 1}) ``` ![MongoDB Drop Index](https://img.kancloud.cn/ff/81/ff816ba6d33b9bff6b77096f7a18e201_494x202.jpg) `nIndexesWas`:顯示執行此命令之前有多少索引 `ok: 1`:表示命令執行成功。 **刪除所有索引:** 要刪除集合的所有索引,我們使用`dropIndexes()`方法。 `dropIndexs()`方法的語法: ```js db.collection_name.dropIndexes() ``` 假設我們想要刪除`studentdata`集合的所有索引。 ```js db.studentdata.dropIndexes() ``` ![MongoDb Dropping all indexes](https://img.kancloud.cn/53/57/5357d7f458b1de3cb49c71cd53924be4_494x170.jpg) 消息“為集合刪除的非`_id`索引”表示默認索引`_id`仍將保留,不能刪除。這意味著使用此方法我們只能刪除我們創建的索引,我們不能刪除在`_id`字段上創建的默認索引。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看