关于MongoDB索引的介绍可以看一下我的这篇文章:
https://blog.csdn.net/Jifu_M/article/details/112578054
项目介绍
我们拥有一个bookshop.js的文件,将文件导入的MongoDB后按照要求进行操作。
练习用的bookshop.js已被上传到我的资源。
https://download.csdn.net/download/Jifu_M/14159088
为下面4个查询语句创建索引:
(1) 查找具有给定书名的图书的标题、出版商、出版年份和价格。
(2) 查找具有给定作者姓名的书籍的标题、出版商、出版年份和价格。
(3) 使用给定的关键字查找图书的标题、出版商、出版年份和价格。
(4) 查找给定年份的书籍的标题、出版商、出版年份和价格。
对于每个查询,做4步工作:
- 创建一个索引,以加速与模式一致的查询处理
- 使用getIndexes()列出所有现有的索引,例如db.collection.getIndexes()。
- 应用一个explain()来验证系统是否计划使用我们创建的索引来查询。
- 使用dropIndex()删除步骤1中创建的索引。
项目开始
首先要创建一个文件夹来进行项目,之后启动MongoDB,最后读取文件:
mkdir DATA
mongod –dbpath DATA –port 4000
mongo –port 4000
load("bookshop.js");
(1)
db.bookshop.createIndex( {"book.title": 1},{"unique": false} );
db.bookshop.getIndexes();
db.bookshop.find({"book.title":"Database Systems"},
{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
db.bookshop.dropIndex("book.title_1");
运行结果如下:
> db.bookshop.createIndex( {"book.title": 1},{"unique": false} );
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.bookshop.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.bookshop"
},
{
"v" : 2,
"key" : {
"book.title" : 1
},
"name" : "book.title_1",
"ns" : "test.bookshop"
}
]
> db.bookshop.find({"book.title":"Database Systems"},{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.bookshop",
"indexFilterSet" : false,
"parsedQuery" : {
"book.title" : {
"$eq" : "Database Systems"
}
},
"queryHash" : "14A0A730",
"planCacheKey" : "39101E2A",
"winningPlan" : {
"stage" : "PROJECTION_DEFAULT",
"transformBy" : {
"_id" : 0,
"book.title" : 1,
"book.publisher" : 1,
"book.year" : 1,
"book.price" : 1
},
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"book.title" : 1
},
"indexName" : "book.title_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"book.title" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"book.title" : [
"[\"Database Systems\", \"Database Systems\"]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "DESKTOP-UFKEQ4V",
"port" : 27017,
"version" : "4.2.1",
"gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
},
"ok" : 1
}
> db.bookshop.dropIndex("book.title_1");
{ "nIndexesWas" : 2, "ok" : 1 }
(2)
db.bookshop.createIndex( {"book.authors.fname": 1, "book.authors.lname":1},{"unique": false} );
db.bookshop.getIndexes();
db.bookshop.find({"book.authors.fname":"Horstmann","book.authors.lname":"Cornell"},
{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
db.bookshop.dropIndex("book.authors.fname_1_book.authors.lname_1");
运行结果如下:
> db.bookshop.createIndex( {"book.authors.fname": 1, "book.authors.lname":1},{"unique": false} );
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.bookshop.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.bookshop"
},
{
"v" : 2,
"key" : {
"book.authors.fname" : 1,
"book.authors.lname" : 1
},
"name" : "book.authors.fname_1_book.authors.lname_1",
"ns" : "test.bookshop"
}
]
> db.bookshop.find({"book.authors.fname":"Horstmann","book.authors.lname":"Cornell"},{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.bookshop",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"book.authors.fname" : {
"$eq" : "Horstmann"
}
},
{
"book.authors.lname" : {
"$eq" : "Cornell"
}
}
]
},
"queryHash" : "567B5B84",
"planCacheKey" : "B395D723",
"winningPlan" : {
"stage" : "PROJECTION_DEFAULT",
"transformBy" : {
"_id" : 0,
"book.title" : 1,
"book.publisher" : 1,
"book.year" : 1,
"book.price" : 1
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"book.authors.lname" : {
"$eq" : "Cornell"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"book.authors.fname" : 1,
"book.authors.lname" : 1
},
"indexName" : "book.authors.fname_1_book.authors.lname_1",
"isMultiKey" : true,
"multiKeyPaths" : {
"book.authors.fname" : [
"book.authors"
],
"book.authors.lname" : [
"book.authors"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"book.authors.fname" : [
"[\"Horstmann\", \"Horstmann\"]"
],
"book.authors.lname" : [
"[MinKey, MaxKey]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "DESKTOP-UFKEQ4V",
"port" : 27017,
"version" : "4.2.1",
"gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
},
"ok" : 1
}
> db.bookshop.dropIndex("book.authors.fname_1_book.authors.lname_1");
{ "nIndexesWas" : 2, "ok" : 1 }
(3)
db.bookshop.createIndex( {"book.keywords": 1},{"unique": false, "sparse":true} );
db.bookshop.getIndexes();
db.bookshop.find({"book.keywords":"Database"},
{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
db.bookshop.dropIndex("book.keywords_1");
运行结果如下:
> db.bookshop.createIndex( {"book.keywords": 1},{"unique": false, "sparse":true} );
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.bookshop.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.bookshop"
},
{
"v" : 2,
"key" : {
"book.keywords" : 1
},
"name" : "book.keywords_1",
"ns" : "test.bookshop",
"sparse" : true
}
]
> db.bookshop.find({"book.keywords":"Database"},{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.bookshop",
"indexFilterSet" : false,
"parsedQuery" : {
"book.keywords" : {
"$eq" : "Database"
}
},
"queryHash" : "24DDB67D",
"planCacheKey" : "ABFCDB63",
"winningPlan" : {
"stage" : "PROJECTION_DEFAULT",
"transformBy" : {
"_id" : 0,
"book.title" : 1,
"book.publisher" : 1,
"book.year" : 1,
"book.price" : 1
},
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"book.keywords" : 1
},
"indexName" : "book.keywords_1",
"isMultiKey" : true,
"multiKeyPaths" : {
"book.keywords" : [
"book.keywords"
]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"book.keywords" : [
"[\"Database\", \"Database\"]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "DESKTOP-UFKEQ4V",
"port" : 27017,
"version" : "4.2.1",
"gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
},
"ok" : 1
}
> db.bookshop.dropIndex("book.keywords_1");
{ "nIndexesWas" : 2, "ok" : 1 }
>
(4)
db.bookshop.createIndex( {"book.year": 1},{"unique": false} );
db.bookshop.getIndexes();
db.bookshop.find({"book.year":2015},{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
db.bookshop.dropIndex("book.year_1");
运行结果如下:
> db.bookshop.createIndex( {"book.year": 1},{"unique": false} );
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.bookshop.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.bookshop"
},
{
"v" : 2,
"key" : {
"book.year" : 1
},
"name" : "book.year_1",
"ns" : "test.bookshop"
}
]
> db.bookshop.find({"book.year":2015},{"_id":0,"book.title":1,"book.publisher":1,"book.year":1,"book.price":1}).explain();
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.bookshop",
"indexFilterSet" : false,
"parsedQuery" : {
"book.year" : {
"$eq" : 2015
}
},
"queryHash" : "772840C7",
"planCacheKey" : "674A9A1C",
"winningPlan" : {
"stage" : "PROJECTION_DEFAULT",
"transformBy" : {
"_id" : 0,
"book.title" : 1,
"book.publisher" : 1,
"book.year" : 1,
"book.price" : 1
},
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"book.year" : 1
},
"indexName" : "book.year_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"book.year" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"book.year" : [
"[2015.0, 2015.0]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "DESKTOP-UFKEQ4V",
"port" : 27017,
"version" : "4.2.1",
"gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
},
"ok" : 1
}
> db.bookshop.dropIndex("book.year_1");
{ "nIndexesWas" : 2, "ok" : 1 }