Database Reference
In-Depth Information
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.
The following example limits the items from the Cast list to the first three items:
> db.media.find({"Title" : "Matrix, The"}, {"Cast" : {$slice: 3}})
{ "_id" : ObjectId("4c5fcd3edb290000000067cb"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carrie-Anne
Moss", "Laurence Fishburne" ] }
You can also get only the last three items by making the integer negative:
> db.media.find({"Title" : "Matrix, The"}, {"Cast" : {$slice: -3}})
{ "_id" : ObjectId("4c5fcd3edb290000000067cb"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999, "Cast" : [ "Hugo Weaving", "Gloria
Foster",
"Joe Pantoliano" ] }
Or you can skip the first two items and limit the results to three from that particular
point (pay careful attention to the brackets):
> db.media.find({"Title" : "Matrix, The"}, {"Cast" : {$slice: [2,3] }})
{ "_id" : ObjectId("4c5fcd3edb290000000067cb"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999, "Cast" : [ "Laurence Fishburne", "Hugo
Weaving", "Gloria Foster" ] }
 
Search WWH ::




Custom Search