Database Reference
In-Depth Information
Matching All Attributes in a Document
The $all operator also works similarly to the $in operator. However, $all requires that all attributes match in the
documents, whereas only one attribute must match for the $in operator. Let's look at an example that illustrates these
differences. First, here's an example that uses $in :
> db.media.find ( { Released : {$in : ["2010","2009"] } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c4369a3c603000000007ed3"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
One document is returned for the $in operator because there's a match for 2010, but not for 2009. However,
the $all parameter doesn't return any results, because there are no matching documents with 2009 in the value:
> db.media.find ( { Released : {$all : ["2010","2009"] } }, { "Cast" : 0 } )
Searching for Multiple Expressions in a Document
You can use the $or operator to search for multiple expressions in a single query, where only one criterion needs
to match to return a given document. Unlike the $in operator, $or allows you to specify both the key and the value,
rather than only the value:
> db.media.find({ $or : [ { "Title" : "Toy Story 3" }, { "ISBN" :
"987-1-4302-3051-9" } ] } )
{ "_id" : ObjectId("4c5fc7d8db290000000067c5"), "Type" : "Book", "Title" :
"Definitive Guide to MongoDB, The", "ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress", "Author" : ["Hows, David", "Membrey, Peter", "Plugge, Eelco",
"Hawkins, Tim" ] }
{ "_id" : ObjectId("4c5fc943db290000000067ca"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
It's also possible to combine the $or operator with another query parameter. This will restrict the returned
documents to only those that match the first query (mandatory), and then either of the two key/value pairs specified
at the $or operator, as in this example:
> db.media.find({ "Type" : "DVD", $or : [ { "Title" : "Toy Story 3" }, {
"ISBN" : "987-1-4302-3051-9" } ] })
{ "_id" : ObjectId("4c5fc943db290000000067ca"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
You could say that the $or operator allows you to perform two queries at the same time, combining the results of
two otherwise unrelated queries.
Retrieving a Document with $slice
You can use the $ slice operator to retrieve a document that includes a specific area from an array in that document.
This can be particularly useful if you want to limit a certain set of items added to save bandwidth. The operator also
lets you retrieve the results n items per page, a feature generally known as paging .
In theory, the $slice operator combines the capabilities of the limit() and skip() functions; however, limit()
and skip() do not work on an array, whereas $slice does. The operator takes two parameters; the first indicates the
total number of items to be returned. The second parameter is optional; if used, it ensures that the first parameter
defines the offset, while the second defines the limit. The limit parameter can also indicate a negative condition.
 
Search WWH ::




Custom Search