- MongoDB Administrator’s Guide
- Cyrus Dasadia
- 440字
- 2021-07-02 15:47:47
Getting ready
Assuming that you are already running a MongoDB server, we will be importing a dataset of around 100,000 records available in the form of a CSV file called chapter_2_mock_data.csv. You can download this file from the Packt website.
- Import the sample data to the MongoDB server:
$mongoimport --headerline --ignoreBlanks --type=csv -d mydb -c mockdata -h localhost chapter_2_mock_data.csv
You should see output like this:
2017-06-18T08:25:08.444+0530 connected to: localhost
2017-06-18T08:25:09.498+0530 imported 100000 documents
- Connect to the MongoDB instance and open a mongo shell:
mongo localhost:27017
- Check that the documents are in the right place:
use mydb
db.mockdata.count()
You should see the following result:
105000
- Let's fetch a document with the explain() method:
> db.mockdata.find({city:'Singapore'}).explain("executionStats")
You should see the following result:
{
"executionStats": {
"executionStages": {
"advanced": 1,
"direction": "forward",
"docsExamined": 100000,
"executionTimeMillisEstimate": 44,
"filter": {
"city": {
"$eq": "Singapore"
}
},
"invalidates": 0,
"isEOF": 1,
"nReturned": 1,
"needTime": 100000,
"needYield": 0,
"restoreState": 783,
"saveState": 783,
"stage": "COLLSCAN",
"works": 100002
},
"executionSuccess": true,
"executionTimeMillis": 41,
"nReturned": 1,
"totalDocsExamined": 100000,
"totalKeysExamined": 0
},
"ok": 1,
"queryPlanner": {
"indexFilterSet": false,
"namespace": "mydb.mockdata",
"parsedQuery": {
"city": {
"$eq": "Singapore"
}
},
"plannerVersion": 1,
"rejectedPlans": [],
"winningPlan": {
"direction": "forward",
"filter": {
"city": {
"$eq": "Singapore"
}
},
"stage": "COLLSCAN"
}
},
"serverInfo": {
"gitVersion": "888390515874a9debd1b6c5d36559ca86b44babd",
"host": "vagrant-ubuntu-trusty-64",
"port": 27017,
"version": "3.4.4"
}
}
- Create an index on the city field:
> db.mockdata.createIndex({'city': 1})
The following result is obtained:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
- Execute the same fetch query:
> db.mockdata.find({city:'Singapore'}).explain("executionStats")
{
"executionStats": {
"executionStages": {
"advanced": 1,
"alreadyHasObj": 0,
"docsExamined": 1,
"executionTimeMillisEstimate": 0,
"inputStage": {
"advanced": 1,
"direction": "forward",
"dupsDropped": 0,
"dupsTested": 0,
"executionTimeMillisEstimate": 0,
"indexBounds": {
"city": [
"[\"Singapore\", \"Singapore\"]"
]
},
"indexName": "city_1",
"indexVersion": 2,
"invalidates": 0,
"isEOF": 1,
"isMultiKey": false,
"isPartial": false,
"isSparse": false,
"isUnique": false,
"keyPattern": {
"city": 1
},
"keysExamined": 1,
"multiKeyPaths": {
"city": []
},
"nReturned": 1,
"needTime": 0,
"needYield": 0,
"restoreState": 0,
"saveState": 0,
"seeks": 1,
"seenInvalidated": 0,
"stage": "IXSCAN",
"works": 2
},
"invalidates": 0,
"isEOF": 1,
"nReturned": 1,
"needTime": 0,
"needYield": 0,
"restoreState": 0,
"saveState": 0,
"stage": "FETCH",
"works": 2
},
"executionSuccess": true,
"executionTimeMillis": 0,
"nReturned": 1,
"totalDocsExamined": 1,
"totalKeysExamined": 1
},
"ok": 1,
"queryPlanner": {
"indexFilterSet": false,
"namespace": "mydb.mockdata",
"parsedQuery": {
"city": {
"$eq": "Singapore"
}
},
"plannerVersion": 1,
"rejectedPlans": [],
"winningPlan": {
"inputStage": {
"direction": "forward",
"indexBounds": {
"city": [
"[\"Singapore\", \"Singapore\"]"
]
},
"indexName": "city_1",
"indexVersion": 2,
"isMultiKey": false,
"isPartial": false,
"isSparse": false,
"isUnique": false,
"keyPattern": {
"city": 1
},
"multiKeyPaths": {
"city": []
},
"stage": "IXSCAN"
},
"stage": "FETCH"
}
},
"serverInfo": {
"gitVersion": "888390515874a9debd1b6c5d36559ca86b44babd",
"host": "vagrant-ubuntu-trusty-64",
"port": 27017,
"version": "3.4.4"
}
}
推薦閱讀
- 零基礎學Visual C++第3版
- VMware View Security Essentials
- 國際大學生程序設計競賽中山大學內部選拔真題解(二)
- HTML5+CSS3基礎開發教程(第2版)
- Visual Basic程序設計教程
- Full-Stack React Projects
- WebRTC技術詳解:從0到1構建多人視頻會議系統
- 機器學習與R語言實戰
- 執劍而舞:用代碼創作藝術
- Instant Automapper
- INSTANT Apache Hive Essentials How-to
- 透視C#核心技術:系統架構及移動端開發
- jBPM6 Developer Guide
- Mastering Swift 4(Fourth Edition)
- Learning Rust