In this lab, we will explore the find() statement in MongoDB, including a range of operators used to filter query results.
The find() method with no parameters returns all documents from a collection and returns all fields for the documents. For example, the following operation returns all documents in the bios collection:
db.bios.find()
The following operation returns documents in the bios collection where _id
equals 5:
db.bios.find( { _id: 5 } )
You can specify multiple properties in the find() statement as follows:
db.grades.find(
{class_id: 19,student_id:5}
).pretty()
The .pretty() function displays query results in a more readable format.
You can also use the $and
or $or
operators as follows:
db.grades.find(
{$and: [
{class_id: 19},
{student_id:5}
]}
).pretty()
The query above finds a document where the student id is 5 and the class_id is 19 (just one result). The query below, on the other hand, will return all the documents related to either student 5 or class 19 (so there will be multiple results).
db.grades.find(
{$or: [
{class_id: 19},
{student_id:5}
]}
).pretty()
To find documents that match a set of selection criteria, call find() with the <criteria>
parameter. The following operation returns all the documents from the collection products where qty is greater than 25:
db.products.find( { qty: { $gt: 25 } } )
The following operation returns documents in the bios collection where _id
equals either 5 or ObjectId("507c35dd8fada716c89d0013"):
db.bios.find(
{
_id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] }
}
)
Below is a list of all of the comparison query operators available in mongodb.
Name | Description |
---|---|
$eq | Matches values that are equal to a specified value. |
$gt | Matches values that are greater than a specified value. |
$gte | Matches values that are greater than or equal to a specified value. |
$in | Matches any of the values specified in an array. |
$lt | Matches values that are less than a specified value. |
$lte | Matches values that are less than or equal to a specified value. |
$ne | Matches all values that are not equal to a specified value. |
$nin | Matches none of the values specified in an array. |
Combine comparison operators to specify ranges. The following operation returns documents with field between value1 and value2:
db.collection.find( { field: { $gt: value1, $lt: value2 } } );
If a field contains an array and your query has multiple conditional operators, the field as a whole will match if either a single array element meets the conditions or a combination of array elements meet the conditions.
Given a collection students that contains the following documents:
{ "_id" : 1, "score" : [ -1, 3 ] }
{ "_id" : 2, "score" : [ 1, 5 ] }
{ "_id" : 3, "score" : [ 5, 5 ] }
The following query:
db.students.find( { score: { $gt: 0, $lt: 2 } } )
Matches the following documents:
{ "_id" : 1, "score" : [ -1, 3 ] }
{ "_id" : 2, "score" : [ 1, 5 ] }
In the document with _id
equal to 1, the score: [ -1, 3 ] meets the conditions because the element -1 meets the $lt: 2 condition and the element 3 meets the $gt: 0 condition.
In the document with _id
equal to 2, the score: [ 1, 5 ] meets the conditions because the element 1 meets both the $lt: 2 condition and the $gt: 0 condition.
The following operation returns documents in the bios collection where the array field contribs contains the element "UNIX":
db.bios.find( { contribs: "UNIX" } )
The following operation returns documents in the bios collection where awards array contains an embedded document element that contains the award field equal to "Turing Award" and the year field greater than 1980:
db.bios.find(
{
awards: {
$elemMatch: {
award: "Turing Award",
year: { $gt: 1980 }
}
}
}
)
Solutions for the exercises in this lab are available here: find_solutions.zip