实验一:MongoDB的基本操作

实验目的:

  1. 安装Windows版MongoDB

  2. 使用MongoDB对数据进行增删改查操作

  3. 比较有无索引时的查询性能
    实验内容:

题目1:安装Windows版MongoDB

1.安装MongoDB 4.4 社区版

2.在环境变量中添加MongoDB安装路径

3.创建MongoDB的Windows服务,服务名为MongoDB

4.设置Windows服务为【自动】启动

5.使用mongod命令启动服务

6.使用mongo命令启动shell窗口

最详细的Windows平台安装MongoDB教程
https://www.cnblogs.com/TM0831/p/10606624.html

题目2:对数据进行增删改查操作

1.创建一个数据库

2.创建一个集合

3.插入多条文档记录到集合中

4.修改一个文档的全部内容

5.对多个文档同一字段内容进行修改

6.查询数据库中满足指定条件的文档

题目3:比较有无索引时查询性能

  1. 在集合中插入1000个文档,执行find.explain()命令,记录分析结果

  2. 对文档中的一个字段创建索引,对该字段进行find.explain()命令,记录分析结果;

  3. 删除多余文档,保留200个文档,对创建索引字段执行find.explain()命令,记录分析结果

  4. 在此基础上删除建立的索引,执行find.explain()命令,记录分析结果

  5. 对以上执行结果惊醒统计,制出统计分析比较表,说明比较结论

5.使用mongod命令启动服务
6.使用mongo命令启动shell窗口

实验一:MongoDB的基本操作

实验一:MongoDB的基本操作

1.创建一个数据库

use cww
结果:
switched to db cww

2.创建一个集合

db.createCollection("wds")
结果:
{ "ok" : 1 }

3.插入多条文档记录到集合中

db.wds.insert(

[

{ item: "小学生教材",name:"《小学一年级语文(上册)》",price:12},

{ item: "初中生教材",name:"《初中一年级语文(上册)》",price:15},

{ item: "高中生教材",name:"《高中一年级语文(上册)》",price:20},

{ item: "外语教材",name:"《英语全解\nABC(五年级上)》",price:30}

])
结果:
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 4,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

4.修改一个文档的全部内容

db.wds.update({"item":"小学生教材"},{$set:{"name":"wds"}})
结果:
db.wds.find()
{ "_id" : ObjectId("6170dd4d3e5499c12846577c"), "item" : "小学生教材", "name" : "wds", "price" : 12 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577d"), "item" : "初中生教材", "name" : "《初中一年级语文(上册)》", "price" : 15 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577e"), "item" : "高中生教材", "name" : "《高中一年级语文(上册)》", "price" : 20 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577f"), "item" : "外语教材", "name" : "《英语全解\nABC(五年级上)》", "price" : 30 }
{ "_id" : ObjectId("6170df5d313c45471807211c"), "item" : "小学生教材" }

5.对多个文档同一字段内容进行修改

db.wds.update({"item":"小学生教材"},{$set:{"name":"wds"}},{multi:true} )
结果:
db.wds.find()
{ "_id" : ObjectId("6170dd4d3e5499c12846577c"), "item" : "小学生教材", "name" : "wds", "price" : 12 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577d"), "item" : "初中生教材", "name" : "《初中一年级语文(上册)》", "price" : 15 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577e"), "item" : "高中生教材", "name" : "《高中一年级语文(上册)》", "price" : 20 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577f"), "item" : "外语教材", "name" : "《英语全解\nABC(五年级上)》", "price" : 30 }
{ "_id" : ObjectId("6170df5d313c45471807211c"), "item" : "小学生教材", "name" : "wds" }

6.查询数据库中满足指定条件的文档

 db.wds.find({"item":"小学生教材"})
 结果:
{ "_id" : ObjectId("6170dd4d3e5499c12846577c"), "item" : "小学生教材", "name" : "wds", "price" : 12 }
{ "_id" : ObjectId("6170df5d313c45471807211c"), "item" : "小学生教材", "name" : "wds" }
  1. 在集合中插入1000个文档,执行find.explain()命令,记录分析结果
 for(var i =0;i<=1000;i++){db.wds.insert({"id":i+1,"name":"wds"+i,"age":21})}
 结果:
 db.wds.find()
{ "_id" : ObjectId("6170dd4d3e5499c12846577c"), "item" : "小学生教材", "name" : "wds", "price" : 12 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577d"), "item" : "初中生教材", "name" : "《初中一年级语文(上册)》", "price" : 15 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577e"), "item" : "高中生教材", "name" : "《高中一年级语文(上册)》", "price" : 20 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577f"), "item" : "外语教材", "name" : "《英语全解\nABC(五年级上)》", "price" : 30 }
{ "_id" : ObjectId("6170df5d313c45471807211c"), "item" : "小学生教材", "name" : "wds" }
{ "_id" : ObjectId("6170ea883234079ed825be3e"), "id" : 1, "name" : "wds0", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be3f"), "id" : 2, "name" : "wds1", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be40"), "id" : 3, "name" : "wds2", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be41"), "id" : 4, "name" : "wds3", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be42"), "id" : 5, "name" : "wds4", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be43"), "id" : 6, "name" : "wds5", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be44"), "id" : 7, "name" : "wds6", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be45"), "id" : 8, "name" : "wds7", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be46"), "id" : 9, "name" : "wds8", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be47"), "id" : 10, "name" : "wds9", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be48"), "id" : 11, "name" : "wds10", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be49"), "id" : 12, "name" : "wds11", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be4a"), "id" : 13, "name" : "wds12", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be4b"), "id" : 14, "name" : "wds13", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825be4c"), "id" : 15, "name" : "wds14", "age" : 21 }
Type "it" for more
第二句:
db.wds.find({"id":801}).explain("executionStats")
结果:
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "cww.wds",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "id" : {
                                "$eq" : 801
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "id" : {
                                        "$eq" : 801
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 1006,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "id" : {
                                        "$eq" : 801
                                }
                        },
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 1008,
                        "advanced" : 1,
                        "needTime" : 1006,
                        "needYield" : 0,
                        "saveState" : 1,
                        "restoreState" : 1,
                        "isEOF" : 1,
                        "direction" : "forward",
                        "docsExamined" : 1006
                }
        },
  1. 对文档中的一个字段创建索引,对该字段进行find.explain()命令,记录分析结果;
db.wds.ensureIndex({"id":801})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
db.wds.find({"id":801}).explain("executionStats")
结果:
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "cww.wds",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "id" : {
                                "$eq" : 801
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "id" : 801
                                },
                                "indexName" : "id_801",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "id" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "id" : [
                                                "[801.0, 801.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 1,
                "totalKeysExamined" : 1,
                "totalDocsExamined" : 1,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 2,
                        "advanced" : 1,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "docsExamined" : 1,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 1,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 2,
                                "advanced" : 1,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "keyPattern" : {
                                        "id" : 801
                                },
                                "indexName" : "id_801",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "id" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "id" : [
                                                "[801.0, 801.0]"
                                        ]
                                },
                                "keysExamined" : 1,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "DESKTOP-FP9BBM9",
                "port" : 27017,
                "version" : "4.4.0",
                "gitVersion" : "563487e100c4215e2dce98d0af2a6a5a2d67c5cf"
        },
        "ok" : 1
}
  1. 删除多余文档,保留200个文档,对创建索引字段执行find.explain()命令,记录分析结果
for(var i=0;i<=800;i++){ db.wds.remove({"id":i})}
WriteResult({ "nRemoved" : 1 })
db.wds.find()
结果: 
{ "_id" : ObjectId("6170dd4d3e5499c12846577c"), "item" : "小学生教材", "name" : "wds", "price" : 12 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577d"), "item" : "初中生教材", "name" : "《初中一年级语文(上册)》", "price" : 15 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577e"), "item" : "高中生教材", "name" : "《高中一年级语文(上册)》", "price" : 20 }
{ "_id" : ObjectId("6170dd4d3e5499c12846577f"), "item" : "外语教材", "name" : "《英语全解\nABC(五年级上)》", "price" : 30 }
{ "_id" : ObjectId("6170df5d313c45471807211c"), "item" : "小学生教材", "name" : "wds" }
{ "_id" : ObjectId("6170ea883234079ed825c15e"), "id" : 801, "name" : "wds800", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c15f"), "id" : 802, "name" : "wds801", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c160"), "id" : 803, "name" : "wds802", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c161"), "id" : 804, "name" : "wds803", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c162"), "id" : 805, "name" : "wds804", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c163"), "id" : 806, "name" : "wds805", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c164"), "id" : 807, "name" : "wds806", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c165"), "id" : 808, "name" : "wds807", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c166"), "id" : 809, "name" : "wds808", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c167"), "id" : 810, "name" : "wds809", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c168"), "id" : 811, "name" : "wds810", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c169"), "id" : 812, "name" : "wds811", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c16a"), "id" : 813, "name" : "wds812", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c16b"), "id" : 814, "name" : "wds813", "age" : 21 }
{ "_id" : ObjectId("6170ea883234079ed825c16c"), "id" : 815, "name" : "wds814", "age" : 21 }
Type "it" for more

 db.wds.find({"id":801}).explain("executionStats")
 结果:
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "cww.wds",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "id" : {
                                "$eq" : 801
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "id" : 801
                                },
                                "indexName" : "id_801",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "id" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "id" : [
                                                "[801.0, 801.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 1,
                "totalDocsExamined" : 1,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 2,
                        "advanced" : 1,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "docsExamined" : 1,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 1,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 2,
                                "advanced" : 1,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "keyPattern" : {
                                        "id" : 801
                                },
                                "indexName" : "id_801",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "id" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "id" : [
                                                "[801.0, 801.0]"
                                        ]
                                },
                                "keysExamined" : 1,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "DESKTOP-FP9BBM9",
                "port" : 27017,
                "version" : "4.4.0",
                "gitVersion" : "563487e100c4215e2dce98d0af2a6a5a2d67c5cf"
        },
        "ok" : 1
}
  1. 在此基础上删除建立的索引,执行find.explain()命令,记录分析结果
 db.wds.dropIndex({"id":801})
{ "nIndexesWas" : 2, "ok" : 1 }
结果:
> db.wds.find({"id":801}).explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "cww.wds",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "id" : {
                                "$eq" : 801
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "id" : {
                                        "$eq" : 801
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 206,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "id" : {
                                        "$eq" : 801
                                }
                        },
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 208,
                        "advanced" : 1,
                        "needTime" : 206,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "direction" : "forward",
                        "docsExamined" : 206
                }
        },
        "serverInfo" : {
                "host" : "DESKTOP-FP9BBM9",
                "port" : 27017,
                "version" : "4.4.0",
                "gitVersion" : "563487e100c4215e2dce98d0af2a6a5a2d67c5cf"
        },
        "ok" : 1
}
  1. 对以上执行结果惊醒统计,制出统计分析比较表,说明比较结论
type needtime
1006个数,未定索引 1006
1006个数,定索引{“id”:801} 0
206个数,定索引 0
206个数,未定索引 206
上一篇:2022MongoDB4-find正则等其它用法


下一篇:Cannot autogenerate id of type java.lang.Integer for entity of type com.mongodb.pojo.User!