官术网_书友最值得收藏!

How it works...

In step 1, we used the mongoimport utility to import our sample dataset from chapter_2_mock_data.csv which is a comma separated file. We'll discuss more about mongoimport in later chapters, so don't worry about it for now. Once we import the data, we execute the mongo shell and confirm that we've indeed imported our sample dataset (100,000 documents).

In step 4, we run a simple find() function chained with the explain() function. The explain() function shows us all the details about the execution of our query, especially the executionStats. In this, if you look at the value of key executionStages['stage'], you can see it says COLLSAN. This indicates that the entire collection was scanned, which can be confirmed by looking at the totalDocsExamined key's value, which should say 100,000. Clearly our collection needs an index!

In step 5, we create and index by calling db.mockdata.createIndex({'city': 1}). In createIndex() function, we mention the city field with value of 1 which tells MongoDB to create an ascending index on this key. You can use -1 to create a descending index, if need be. By executing this function, MongoDB immediately begins creating an index on the collection.

Index creation is an intensive blocking call which means database operations will be blocked until the index is created. We will examine how to create background indexes in later recipes, in this chapter.

In step 6, we execute the exact same find() query, as we did in step 4, and upon inspecting the executionStats, you can observe that the value of key executionStages now contains some more details. Especially, the value of stage key is FETCH and the inputStages['stage'] is IXSCAN. In short, this indicates that the query was fetched from by running an index scan. As this was a direct index hit, the value of totalDocsExamined is 1.

主站蜘蛛池模板: 鹤岗市| 河津市| 鄂伦春自治旗| 鄂托克前旗| 漳浦县| 泰安市| 长丰县| 阿拉善盟| 虎林市| 清河县| 保山市| 和平县| 弋阳县| 麻阳| 额敏县| 贵溪市| 正定县| 福贡县| 浦北县| 吉木乃县| 花垣县| 青州市| 中卫市| 青田县| 连南| 阿坝县| 孟村| 阿鲁科尔沁旗| 探索| 海城市| 临漳县| 古交市| 临城县| 霍邱县| 方正县| 屏山县| 曲周县| 永昌县| 八宿县| 武邑县| 白沙|