NoSQL Database by Christof Strauch - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

MongoDB features commands which are sent to the database to gain information about its operational sta- tus or to perform maintainance or administration tasks. A command is executed via the special namespace $cmd and has the following general syntax:

db. $cmd . findOne ( { <commandname >: <value > [, options ] } );

When such a command is sent to a MongoDB server it will answer with a single document containing the command results (cf. [MMM+10e]).

Examples of MongoDB commands include cloning of databases, flushing of pending writes to datafiles on disk, locking and unlocking of datafiles to block and unblock write operations (for backup purposes), creating and dropping of indexes, obtaining information about recent errors, viewing and terminating running operations, validating collections and retrieving statistics as well as database system information (cf. [CHM+10b]).

5.2.5. Indexing

Like relational database systems, MongoDB allows to specify indexes on document fields of a collection. The information gathered about these fields is stored in B-Trees and utilized by the query optimizing component to “to quickly sort through and order the documents in a collection” thereby enhancing read performance.

As in relational databases, indexes accelerate select as well as update operations as documents can be found faster by the index than via a full collection scan; on the other side indexes add overhead to insert and delete operations as the B-tree index has to be updated in addition to the collection itself. Therefore the MongoDB manual concludes that “indexes are best for collections where the number of reads is much greater than the number of writes. For collections which are write-intensive, indexes, in some cases, may be counterproductive” (cf. [MMH+10]). As a general rule, the MongoDB manual suggests to index “fields upon which keys are looked up” as well as sort fields; fields that should get indexed can also be determined using the profiling facility provided by MongoDB or by retrieving an execution plan by invoking the explain operation on a query18 (cf. [MMM+10b, Index Selection], [CMB+10, Optimizing A Simple Example]).

Indexes are created by an operation named ensureIndex:

db.< collection >. ensureIndex ({< field1 >:< sorting >, <field2 >:< sorting >, ...}) ;

Indexes may be defined on fields of any type—even on nested documents19. If only certain fields of nested documents shall get indexed, they can be specified using a dot-notation (i.e. fieldname.subfieldname). If an index is specified for an array field, each element of the array gets indexed by MongoDB. The sorting flag may be set to 1 for an ascending and -1 for a descending order. The sort order is relevant for sorts and range queries on compound indexes (i.e. indexes on multiple fields) according to the MongoDB manual (cf. [MMH+10, Compound Keys Indexes], [MMM+10f]). To query an array or object field for multiple values the $all operator has to be used:

db.< collection >. find ( <field >: { $all : [ <value_1 >, <value_2 > ... ] } );

By default, MongoDB creates an index on the _id field as it uniquely identifies documents in a collection and is expected to be chosen as a selection criterion often; however, when creating a collection manually, the automatic indexing of this field can be neglected. Another important option regarding indexes is background index building which has to be explicitly chosen as by default the index building blocks all other database operations. When using background indexing, the index is built incrementally and which is slower than indexing as a foreground job. Indexes built in background are taken into account for queries only when the background indexing job has finished. The MongoDB manual mentions two limitations with regards to (background) indexing: first, only one index per collection can be built a time; second, certain administrative operations (like repairDatabase) are disabled while the index is built (cf. [MH10b]).

MongoDB also supports unique indexes specifying that no two documents of a collections have the same value for such a unique index field. To specify unique indexes, another parameter is passed to the afore- mentioned ensureIndex operation:

db.< collection >. ensureIndex ({< field1 >:< sorting >, <field2 >:< sorting >, ...} , { unique :

     true });

The MongoDB manual warns that index fields have to be present in all documents of a collection. As MongoDB automatically inserts all missing indexed fields with null values,