做實驗驗證MongoDB刪除文件後索引是否會自動刪除
下面是實驗步驟:
> db.things.find()
{ "_id" : ObjectId("5652d71a1524dc14663060e8"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5652d71a1524dc14663060e9"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5652d71a1524dc14663060ea"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5652d71a1524dc14663060eb"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5652d71a1524dc14663060ec"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5652d71a1524dc14663060ed"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5652d71a1524dc14663060ee"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5652d71a1524dc14663060ef"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("56d6925aec16355d82bc18fd"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("56d6925aec16355d82bc18fe"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("56d6925aec16355d82bc18ff"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("56d6925aec16355d82bc1900"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("56d6925aec16355d82bc1901"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("56d6925aec16355d82bc1902"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("56d6925aec16355d82bc1903"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("56d6925aec16355d82bc1904"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("56d6925aec16355d82bc1905"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("56d6925aec16355d82bc1906"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("56d6925aec16355d82bc1907"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("56d6925aec16355d82bc1908"), "x" : 4, "j" : 12 }
Type "it" for more
> it
{ "_id" : ObjectId("56d6925aec16355d82bc1909"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("56d6925aec16355d82bc190a"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("56d6925aec16355d82bc190b"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("56d6925aec16355d82bc190c"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("56d6925aec16355d82bc190d"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("56d6925aec16355d82bc190e"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("56d6925aec16355d82bc190f"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("56d6925aec16355d82bc1910"), "x" : 4, "j" : 20 }
>
在文件15上建立索引:
> db.things.ensureIndex({"x":4,"j":15})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
在文件16上建立索引,並命名為duansf
> db.things.ensureIndex({"x":4,"j":16},{"name":"duansf"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
>
查索引:
> db.things.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.things",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 15
},
"name" : "x_4_j_15",
"ns" : "test.things"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 16
},
"name" : "duansf",
"ns" : "test.things"
}
]
>
用remove命令刪除所有文件:
> db.things.remove({})
WriteResult({ "nRemoved" : 26 })
>
things集合記錄為空
> db.things.find()
但索引還存在:
> db.things.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.things",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 15
},
"name" : "x_4_j_15",
"ns" : "test.things"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 16
},
"name" : "duansf",
"ns" : "test.things"
}
]
>
清空整個集合:
> db.things.drop()
true
>
>
> db.things.find()
>
>
下面可以看到索引不再存在:
> db.things.getIndexes()
[ ]
>
結論:集合清空後,索引自動刪除
文件刪除後,被刪除文件對應的索引還在
> db.things.find()
{ "_id" : ObjectId("5652d71a1524dc14663060e8"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5652d71a1524dc14663060e9"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5652d71a1524dc14663060ea"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5652d71a1524dc14663060eb"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5652d71a1524dc14663060ec"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5652d71a1524dc14663060ed"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5652d71a1524dc14663060ee"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5652d71a1524dc14663060ef"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("56d6925aec16355d82bc18fd"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("56d6925aec16355d82bc18fe"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("56d6925aec16355d82bc18ff"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("56d6925aec16355d82bc1900"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("56d6925aec16355d82bc1901"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("56d6925aec16355d82bc1902"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("56d6925aec16355d82bc1903"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("56d6925aec16355d82bc1904"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("56d6925aec16355d82bc1905"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("56d6925aec16355d82bc1906"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("56d6925aec16355d82bc1907"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("56d6925aec16355d82bc1908"), "x" : 4, "j" : 12 }
Type "it" for more
> it
{ "_id" : ObjectId("56d6925aec16355d82bc1909"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("56d6925aec16355d82bc190a"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("56d6925aec16355d82bc190b"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("56d6925aec16355d82bc190c"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("56d6925aec16355d82bc190d"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("56d6925aec16355d82bc190e"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("56d6925aec16355d82bc190f"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("56d6925aec16355d82bc1910"), "x" : 4, "j" : 20 }
>
在文件15上建立索引:
> db.things.ensureIndex({"x":4,"j":15})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
在文件16上建立索引,並命名為duansf
> db.things.ensureIndex({"x":4,"j":16},{"name":"duansf"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
>
查索引:
> db.things.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.things",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 15
},
"name" : "x_4_j_15",
"ns" : "test.things"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 16
},
"name" : "duansf",
"ns" : "test.things"
}
]
>
用remove命令刪除所有文件:
> db.things.remove({})
WriteResult({ "nRemoved" : 26 })
>
things集合記錄為空
> db.things.find()
但索引還存在:
> db.things.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.things",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 15
},
"name" : "x_4_j_15",
"ns" : "test.things"
},
{
"v" : 1,
"key" : {
"x" : 4,
"j" : 16
},
"name" : "duansf",
"ns" : "test.things"
}
]
>
清空整個集合:
> db.things.drop()
true
>
>
> db.things.find()
>
>
下面可以看到索引不再存在:
> db.things.getIndexes()
[ ]
>
結論:集合清空後,索引自動刪除
文件刪除後,被刪除文件對應的索引還在
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2028381/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB 刪除文件MongoDB
- mongodb建立索引和刪除索引和背景索引backgroundMongoDB索引
- mysql刪除主鍵索引,刪除索引語法MySql索引
- 【Mongo】MongoDB索引管理-索引的建立、檢視、刪除MongoDB索引
- Elasticsearch 刪除文件Elasticsearch
- 儲存系統實現-資料刪除之索引的刪除索引
- jQuery中點選刪除,顯示是否要刪除jQuery
- 點選刪除彈出提示是否刪除程式碼
- MySQL定期自動刪除表MySql
- 實驗:centos6刪除核心並恢復CentOS
- 實驗2:利用BBED工具刪除行資料
- 【實驗】【PARTITION】RANGE分割槽表刪除分割槽
- MongoDB之資料刪除MongoDB
- js確認是否刪除。JS
- DataFrame刪除複合索引索引
- 點選刪除按鈕彈出是否刪除提示框
- CMU資料庫(15-445)-實驗2-B+樹索引實現(中)刪除資料庫索引
- MongoDB 資料庫建立刪除、表(集合)建立刪除、資料增刪改查MongoDB資料庫
- mongodb刪除重複資料MongoDB
- 10G DATAGUARD增加REDO日誌組、刪除日誌組、刪除日誌成員實驗
- oracle 刪除表空間試驗面面觀Oracle
- Mysql索引的建立與刪除MySql索引
- elasticsearch(三)----索引建立與刪除Elasticsearch索引
- MySQL 5.5 刪除索引的方法MySql索引
- 做實驗驗證MongoDB分頁的兩種方法MongoDB
- word文件打字時會消掉後面字怎麼辦 word打字自動刪除後面的字怎麼解決
- ElasticSearch分片互動過程(建立索引、刪除索引、查詢索引)Elasticsearch索引
- vscode自動刪除行尾空格VSCode
- PostgreSQL表增加/刪除欄位是否會重寫表SQL
- mongodb副本集新增刪除節點MongoDB
- 怎麼刪除mongodb服務項MongoDB
- MongoDB 集合的插入、更新、刪除操作MongoDB
- Mongodb原始碼分析--刪除記錄MongoDB原始碼
- Lucene中建立索引的效率和刪除索引的實現索引
- 使用Java在PowerPoint中新增、驗證或刪除數字簽名Java
- 實驗1:利用BBED工具恢復刪除的資料行
- mysql 建立和刪除聯合索引MySql索引
- Elasticsearch6.5中文文件-刪除文件Elasticsearch