下面是实验步骤:
> 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()
[ ]
>
结论:集合清空后,索引自动删除
文档删除后,被删除文档对应的索引还在
最新评论