Read (find)

In this lab, we will explore the find() statement in MongoDB, including a range of operators used to filter query results.

find() queries

Find All Documents in a Collection

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()

Query for Equality

The following operation returns documents in the bios collection where _id equals 5:

db.bios.find( { _id: 5 } )

Query on Multiple Properties

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.

And/Or Operators

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()

Find Documents that Match Query Criteria

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") ] }
   }
)

Comparison Query Operators

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.

Ranges and Arrays

Query for Ranges

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 } } );

Query a Field that Contains an Array

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.

Query for an Array Element

The following operation returns documents in the bios collection where the array field contribs contains the element "UNIX":

db.bios.find( { contribs: "UNIX" } )

Query an Array of Documents

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 }
                }
      }
   }
)

Exercises

  1. Find all documents that relate to student 4 and class 0.
  2. Find all documents that relate to either student 2 or class 23.
  3. Use the $in operator to find all documents that relate to student 28 and class 23 or 29.
  4. Use the $nin operator to find results for student 40, excluding class 1, 2, 14 and 22.
  5. Use the $eq to find student 5.
  6. Find all the documents that relate to class 18 where the student number is not 30. Use the $ne operator.
  7. Find scores greater than 99 and less than 100 (hint: you are querying an array of documents).
  8. Find quiz scores greater than or equal to 35 and less than or equal to 40.

Solutions

Solutions for the exercises in this lab are available here: find_solutions.zip