|
Create, Read, Update and Delete Operations
Performing Queries: Overview
How do you establish query criteria?
.
- Criteria consists of one or more fields
- Use the minimum number of fields to identify the document
- Demo: customers collection
What query operators are available?
.
- Query operators modify the query
- Comparison: $gt, $gte, $in, $lt, $lte, $ne, $nin
- Logical: $and, $or, $not, $nor
- Element: $exists, $type
- Evaluation: $mod, $regex, $where
- Geospatial: $geoWithin, $geoIntersects, $near, $nearSphere
- Array: $all, $elemMatch, $size
What are projections?
.
- find( { query }, { projection } )
- Allows you to specify which fields are returned
- In the projection document, 1 = include, 0 = exclude
- find( {query},{name:1,_id:0})
What projection operators are available?
.
What about modifiers?
.
- Included in the query:
- $comment, $explain, $hint, $maxScan, $max, $min
- $orderBy, $returnKey, $showDiskLoc, $snapshot, $query, $natural
- Outside the query:
- explain(), hint(), limit(), max(), min(), sort(), snapshot()
How can you optimize queries?
.
- Create an index to cover fields used in query
- db.collection.ensureIndex({field:1})
- 1 = ascending, -1 = descending
- Use projections and limit() to limit fields and documents returned
- Use explain() to analyze performance
- Use hint() to force MongoDB to use a particular index
|
|