Database Reference
In-Depth Information
Likewise, you can find any documents containing an uneven value in the
Released
field by changing the
parameters in
$mod
, as follows:
> db.media.find ( { Released : { $mod: [2,1] } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c45b5b38e0f0000000062a9"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999 }
■
the
$mod
operator only works on integer values, not on strings that contain a numbered value. For example,
you can't use the operator on
{ Released : "2010" }
, because it's in quotes and therefore a string.
Note
Filtering Results with $size
The
$size
operator lets you filter your results to match an array with the specified number of elements in it. For
example, you might use this operator to do a search for those CDs that have exactly two songs on them:
> db.media.find ( { Tracklist : {$size : 2} } )
{ "_id" : ObjectId("4c1a86bb2955000000004076"), "Type" : "CD", "Artist" :
"Nirvana", "Title" : "Nevermind", "Tracklist" : [
{
"Track" : "1",
"Title" : "Smells Like Teen Spirit",
"Lenght" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
] }
■
You cannot use the
$size
operator to find a range of sizes. For example, you cannot use it to find arrays with
more than one element in them.
Note
Returning a Specific Field Object
The
$exists
operator allows you to return a specific object if a specified field is either missing or found. The following
example returns all items in the collection with a key named
Author
:
> db.media.find ( { Author : {$exists : true } } )
Similarly, if you invoke this operator with a value of
false
, then all documents that don't have a key named
Author
will be returned:
> db.media.find ( { Author : {$exists : false } } )
■
Warning
Currently, the
$exists
operator is unable to use an index; therefore, using it requires a full table scan.