Database Reference
In-Depth Information
functions or use them separately. Let's begin by adding a few documents that enable you
to take advantage of these functions. First, create an index on the Released field:
> db.media.insert( { "Type" : "DVD", "Title" : "Matrix, The", "Released" :
1999} )
> db.media.insert( { "Type" : "DVD", "Title" : "Blade Runner", "Released" :
1982 } )
> db.media.insert( { "Type" : "DVD", "Title" : "Toy Story 3", "Released" :
2010} )
> db.media.ensureIndex( { "Released": 1 } )
You can now use the max() and min() commands, as in this example:
> db.media.find() . min ( { Released: 1995 } ) . max ( { Released : 2005 } )
{ "_id" : ObjectId("4c45b5b38e0f0000000062a9"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999 }
If no index is created, then an error message will be returned, saying that no index
has been found for the specified key pattern. Obviously, you will need to define which
index must be used with the hint() function:
> db.media.find() . min ( { Released: 1995 } ) .
max ( { Released : 2005 } ). hint ( { Released : 1 } )
{ "_id" : ObjectId("4c45b5b38e0f0000000062a9"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999 }
the min() value will be included in the results, whereas the max() value will be
excluded from the results.
Note
Generally speaking, it is recommended that you use $gt and $lt (greater than and
less than, respectively) rather than min() and max() because $gt and $lt don't require an
index. The min() and max() functions are used primarily for compound keys.
Summary
In this chapter, we've taken a look at the most commonly used commands and options
that can be performed with the MongoDB shell to manipulate data. We also examined
how to search for, add, modify, and delete data, and how to modify your collections and
databases. Next, we took a quick look at atomic operations, how to use aggregation, and
when to use operators such as $elemMatch . Finally, we explored how to create indexes
and when to use them. We examined what indexes are used for, how you can drop them,
how to search for your data using the indexes created, and how to check for running
indexing operations.
In the next chapter, we'll look into the fundamentals of GridFS, including what it is,
what it does, and how it can be used to your benefit.
 
 
Search WWH ::




Custom Search