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]).
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]):
{ age: { $mod : [2, 1]} } // to retrieve documents with an uneven age
{ categories : {$in: [" NoSQL ", " Document Databases "]} }
{ categories : { $all : [" NoSQL ", " Document Databases "]} }
{ categories : { $size : 2} }
{ categories : { $exists : false }, body : { $exists : true } }
Logical junctions can be specified in query objects as follows:
{ $or: [ { reviewed : { $exists : true } }, { categories : { $size : 2} } ] }
{ $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]):
{ categories : " NoSQL " }
It is also possible to specify the position of an element inside an array: {<field>.<index>.<field>: <value>}