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.

The MongoDB manual points out that the field $db is optional and not supported by many programming language drivers at the moment (cf. [MDC+10]).

The MongoDB points out that although references between documents are possible there is the alternative to nest documents within documents. The embedding of documents is “much more efficient” according to the MongoDB manual as “[data] is then colocated on disk; client-server turnarounds to the database are eliminated”. Instead when using references, “each reference traversal is a query to the database” which results at least in an addition of latency between the web or application server and the database but typically more as the referenced data is typically not cached in RAM but has to be loaded from disk.

The MongoDB manual gives some guidance when to reference an object and when to embed it as presented in table 5.1 (cf. [MMM+10b]).

img42.png

Table 5.1.: MongoDB Referencing vs. Embedding Objects (cf. [MMM+10b])

5.2.4. Database Operations Queries

Selection Queries in MongoDB are specified as query objects, BSON documents containing selection criteria11, and passed as a parameter to the find operation which is executed on the collection to be queried (cf. [CMH+10]):

db.< collection >. find ( { title : " MongoDB " );

The selection criteria given to the find operation can be seen as an equivalent to the WHERE clause in SQL statements12 (cf. [Mer10f]). If the query object is empty, all documents of a collection are returned.

In the selection criteria passed to the find operation a lot of operators are allowed—besides equality comparisons as in the example above. These have the following general form:

<fieldname >: {$< operator >: <value >}

<fieldname >: {$< operator >: <value >, $< operator >: value } // AND - junction

The following comparison operators are allowed (cf. [MMM+10c]):

  • Non-equality: $ne
  • Numerical Relations: $gt, $gte, $lt, $lte (representing >, img43.png, <, img44.png)
  • Modulo with divisor and the modulo compare value in a two-element array, e.g.

{ age: { $mod : [2, 1]} } // to retrieve documents with an uneven age

  • Equality-comparison to (at least) one element of an array: $in with an array of values as comparison operand, e.g.

{ categories : {$in: [" NoSQL ", " Document Databases "]} }

  • Non-equality-comparison to all elements of an array: $nin with an array of values as comparison operand
  • Equality-comparison to all elements of an array: $all, e.g.

{ categories : { $all : [" NoSQL ", " Document Databases "]} }

  • Size of array comparison: $size, e.g.

{ categories : { $size : 2} }

  • (Non-)Existence of a field: $exists with the parameter true or false, e.g.

{ categories : { $exists : false }, body : { $exists : true } }

  • Field type: $type with a numerical value for the BSON data typ (as specified in the MongoDB manual, cf. [MMM+10c, Conditional Operators $type])

Logical junctions can be specified in query objects as follows:

  • Comparison expressions that are separated by comma specify an AND-junction.
  • OR-junctions can be defined by the special $or operator that is assigned to an array of booleans or expressions, each of which can satisfy the query. As an example, the following query object matches documents that either are reviewed or have exactly two categories assigned:

{ $or: [ { reviewed : { $exists : true } }, { categories : { $size : 2} } ] }

  • To express NOR-junctions the $nor operator is supported by MongoDB. Like $or it is assigned to an array of expressions or boolean values.
  • Via the $not operator a term can be negated, e.g.

{ $not : { categories : {$in: {" NoSQL "}}} } // category does not contain " NoSQL "

{ $not : { title : /^ Mongo /i} } // title does not start with " Mongo "

The following remarks are made by the MongoDB manual regarding certain field types (cf. [MMM+10c]):

  • Besides the abovementioned comparison operators it is also possible to do (PCRE13) regular expression matching for string fields. If the regular expression only consists a prefix check (/ˆprefix/ [modifiers] equivalent to SQL’s LIKE ’prefix%’) MongoDB uses indexes that are possibly defined on that field.
  • To search for a single value inside an array the array field can simply be assigned to the desired value in the query object, e.g.

{ categories : " NoSQL " }

It is also possible to specify the position of an element inside an array: {<field>.<index>.<field>: <value>}

  • If multiple selection criteria are specified for an array field, it has to be distinguished whether documents have to fulfill all or one of these criteria. If the criteria are separated by comma, each criterion