2020年11月6日 星期五

MongoDB 07.createIndex

無論是關聯式資料庫或是NoSQL
Index都相當重要
尤其在資料量大時特別顯著
MongoDB如何建立Index
目前使用Robo 3T來連線,右鍵資料庫 > Open Shell
語法如下
降冪-1升冪 1
1.單一欄位
db.collectionName.createIndex({"age":1})
2.多重欄位
db.collectionName.createIndex({"name":-1,"age":1})
也可以使用Robo 3T > Collection上右鍵 > Add Index
經過測試百萬筆資料後有Index跟無Index速度真的差很多
參考來源
db.collection.createIndex()
MongoDB 索引