Database Reference
In-Depth Information
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 } } )
■
Currently, the
$exists
operator is unable to use an index; therefore, using it
requires a full table scan.
Warning