如何在 MongoDB 中一次構建多個索引?


要一次構建多個索引,你需要使用 createIndexes() 並將多個鍵傳遞給一個數組。以下是在一次構建多個索引的查詢。

>db.multipleIndexesDemo.createIndexes([{"First":1},{"Second":1},{"Third":1},{"Fourth":1},{"Fifth":1}]);

該查詢將產生以下輸出

{
   "createdCollectionAutomatically" : true,
   "numIndexesBefore" : 1,
   "numIndexesAfter" : 6,
   "ok" : 1
}

現在獲取所有索引

> db.multipleIndexesDemo.getIndexes();

該查詢將產生以下輸出

[
   {
      "v" : 2,
      "key" : {
         "_id" : 1
      },
      "name" : "_id_",
      "ns" : "test.multipleIndexesDemo"
   },
   {
      "v" : 2,
      "key" : {
         "First" : 1
      },
      "name" : "First_1",
      "ns" : "test.multipleIndexesDemo"
   },
   {
      "v" : 2,
      "key" : {
         "Second" : 1
      },
      "name" : "Second_1",
      "ns" : "test.multipleIndexesDemo"
   },
   {
      "v" : 2,
      "key" : {
         "Third" : 1
      },
      "name" : "Third_1",
      "ns" : "test.multipleIndexesDemo"
   },
   {
      "v" : 2,
      "key" : {
         "Fourth" : 1
      },
      "name" : "Fourth_1",
      "ns" : "test.multipleIndexesDemo"
   },
   {
      "v" : 2,
      "key" : {
         "Fifth" : 1
      },
      "name" : "Fifth_1",
      "ns" : "test.multipleIndexesDemo"
   }
]

更新時間: 30-Jul-2019

2K+ 瀏覽

開啟你的 職業生涯

完成課程取得認證

開始
廣告
© . All rights reserved.