Remove 以及dorp做实验验证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上創(chuàng)建索引:
> db.things.ensureIndex({"x":4,"j":15})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
在文檔16上創(chuàng)建索引,并命名為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()
[ ]
>
結(jié)論:集合清空后,索引自動刪除
文檔刪除后,被刪除文檔對應的索引還在
總結(jié)
以上是生活随笔為你收集整理的Remove 以及dorp做实验验证MongoDB删除文档后索引是否会自动删除的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在sublime中直接运行python代
- 下一篇: libusb开发指南