- MongoDB Administrator’s Guide
- Cyrus Dasadia
- 279字
- 2021-07-02 15:47:49
How it works…
We start with loading the sample dataset with an index on the city field. Next, we execute a find() command on our collection chained with the explain('executionStats') function, in steps 2 and 3 respectively. This time, we capture the output of the data in a variable so it is easier to examine for later use.
In step 4, we specifically examine the execution stats. We can observe that nine documents were fetched from the index in which we had one match. When we ran db.mockdata.find({city:'Boston', first_name: 'Sara'}), MongoDB first saw that the city field already has an index on it. So, for the remaining part of the query, MongoDB simply searched the documents which were returned from the index and searched on the field first_name in these documents until they matched the value Sara.
In step 5, we remove the existing index on field city and in step 6, we create a compound index on two field names city and first_name. At this point, I would like to point, that the sequence of the field names is extremely important. As I explained in the introduction of this recipe, compound indexes in MongoDB are created in the order in which the field names are mentioned. Hence, when we create a compound index with, say, {city:1, first_name:1}, MongoDB first creates a B-tree index on the field city and an ascending order followed by first_name in an ascending order.
In step 7, we run the same find() query and examine the executionStats. We can observe that this time, as both keys were indexed, totalDocumentsExamined was 1 that is, we got an exact match in our compound index.
- C#完全自學(xué)教程
- PyTorch自然語(yǔ)言處理入門與實(shí)戰(zhàn)
- 看透JavaScript:原理、方法與實(shí)踐
- C語(yǔ)言程序設(shè)計(jì)立體化案例教程
- Essential Angular
- Learning Laravel 4 Application Development
- QTP自動(dòng)化測(cè)試進(jìn)階
- 零基礎(chǔ)學(xué)Python數(shù)據(jù)分析(升級(jí)版)
- Asynchronous Android Programming(Second Edition)
- 蘋果的產(chǎn)品設(shè)計(jì)之道:創(chuàng)建優(yōu)秀產(chǎn)品、服務(wù)和用戶體驗(yàn)的七個(gè)原則
- Advanced Express Web Application Development
- Canvas Cookbook
- Getting Started with Polymer
- 從零開始學(xué)Selenium自動(dòng)化測(cè)試:基于Python:視頻教學(xué)版
- Apache Solr for Indexing Data