Boston University










<selection> is an optional document that specifies one or more selection criteria<projection> is an optional document that specifies which fields should be returned


$and, $or, $not, $nor
$in, $nin
$in/$nin is generally more efficient than $or/$nor$exists
$and is needed if the subconditions involve the same field
* wildcard character to indicate 0 or more characters.
* wildcard by default on either end of the pattern.
/Boston,/ is the same as /*Boston,*/^ to match the beginning of the value, $ to match the end of the value
/Boston,/ would match “South Boston, Mass”/^Boston,/ would not, because the ^ indicates “Boston” must be at the start of the valuefinds all documents in which val is at least one of the elements in the array associated with arrayField
> db.movies.find( { "actors.name": "Tom Hanks"} )
{ _id: "0107818", name: "Philadelphia", year: 1993,
rating: "PG-13", runtime: 125, genre: "D"
actors: [ { id: "0000158",
name: "Tom Hanks" },
{ id: "0000243",
name: "Denzel Washington" },
...
],
directors: [ { id: "0001129",
name: "Jonathan Demme" } ]
}
{ _id: "0109830", name: "Forrest Gump", year: 1994,
rating: "PG-13", runtime: 142, genre: "CD"
actors: [ { id: "0000158",
name: "Tom Hanks" },
...fieldname:value pairs:
_id field is returned unless you explicitly exclude it.> db.movies.find({ rating: "R", year: 2011 },
{ name: 1 })
{ "_id" : "1411697", "name" : "The Hangover Part II" }
{ "_id" : "1478338", "name" : "Bridesmaids" }
{ "_id" : "1532503", "name" : "Beginners" }
> db.movies.find({ rating: "R", year: 2011 },
{ name: 1, _id: 0 })
{ "name" : "The Hangover Part II" }
{ "name" : "Bridesmaids" }
{ "name" : "Beginners" }_idit to continue the iteration<selection>)
<field>, <selection>)








$project – include, exclude, rename, or create fields
name (1 = include, as in earlier projection documents)pob, which is renamed whereBornyearBorn, which is derived from the existing dob values (yyyy-m-d → yyyy)_id field, because we didn’t exclude it$group – like GROUP BY in SQL{ $sum: 1 } is equivalent to COUNT(*) in SQL
$sum is one example of an accumulator$min, $max, $avg, $addToSet$match – selects documents according to some criteria<selection> has identical syntax to the selection documents used by db.collection.find()



$match: select movies released in 2013$project: for each such movie, create a document with:
_id field$unwind: turn each movie’s document into a set of documents, one for each actor in the array of actors
