Filter MongoDB Collections

MongoDBBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively filter MongoDB collections using various query techniques. The lab covers applying multiple conditions, using comparison operators, matching with regular expressions, checking for field existence, and finding null values. These skills are essential for precise data retrieval in MongoDB. The step-by-step instructions provide practical examples to help you master the fundamentals of MongoDB querying.

Apply Multiple Conditions

In this step, you will learn to apply multiple conditions to filter documents in a MongoDB collection. This allows for more precise queries by combining several criteria.

First, open your terminal and start the MongoDB Shell. This interactive shell is where you will execute all your database commands.

mongosh

Once inside the mongosh shell, you will see a > prompt. Let's switch to a new database called school_database and create a students collection with some sample data.

use school_database
db.students.insertMany([
  { name: "Alice", age: 22, grade: "A", major: "Computer Science" },
  { name: "Bob", age: 20, grade: "B", major: "Mathematics" },
  { name: "Charlie", age: 25, grade: "A", major: "Physics" },
  { name: "David", age: 19, grade: "C", major: "Computer Science" }
]);

This command inserts four documents into the students collection. Now, let's find students who meet multiple conditions. We will use the $and operator to find students who are 20 years or older AND have a grade of "A".

db.students.find({
  $and: [{ age: { $gte: 20 } }, { grade: "A" }]
});

This query uses $gte, which stands for "greater than or equal to", and $and to ensure both conditions are met. The output will show Alice and Charlie:

[
  {
    _id: ObjectId("..."),
    name: 'Alice',
    age: 22,
    grade: 'A',
    major: 'Computer Science'
  },
  {
    _id: ObjectId("..."),
    name: 'Charlie',
    age: 25,
    grade: 'A',
    major: 'Physics'
  }
]

Next, let's use the $or operator to find documents that match at least one of several conditions. This query finds students who are either majoring in "Computer Science" OR are younger than 21.

db.students.find({
  $or: [{ major: "Computer Science" }, { age: { $lt: 21 } }]
});

This query uses $lt, which means "less than". The result will include Alice and David (Computer Science majors) and Bob and David (under 21). Since David matches both, he appears once.

[
  {
    _id: ObjectId("..."),
    name: 'Alice',
    age: 22,
    grade: 'A',
    major: 'Computer Science'
  },
  {
    _id: ObjectId("..."),
    name: 'Bob',
    age: 20,
    grade: 'B',
    major: 'Mathematics'
  },
  {
    _id: ObjectId("..."),
    name: 'David',
    age: 19,
    grade: 'C',
    major: 'Computer Science'
  }
]

In the next step, you will explore more comparison operators. For now, you can remain in the mongosh shell.

Use Comparison Operators

In this step, you will use MongoDB's comparison operators to build more sophisticated queries. These operators allow you to filter data based on value comparisons.

You should still be in the mongosh shell from the previous step. First, let's clear the existing collection and insert new data with a credits field to work with.

db.students.drop();
db.students.insertMany([
  {
    name: "Alice",
    age: 22,
    grade: "A",
    major: "Computer Science",
    credits: 45
  },
  { name: "Bob", age: 20, grade: "B", major: "Mathematics", credits: 35 },
  { name: "Charlie", age: 25, grade: "A", major: "Physics", credits: 50 },
  { name: "David", age: 19, grade: "C", major: "Computer Science", credits: 25 }
]);

Now, let's use the "greater than" ($gt) and "less than" ($lt) operators to find students who are older than 20 but younger than 25.

db.students.find({ age: { $gt: 20, $lt: 25 } });

This query will return only the document for Alice, who is 22.

Next, use "greater than or equal to" ($gte) and "less than or equal to" ($lte) to find students with credits between 40 and 50, inclusive.

db.students.find({ credits: { $gte: 40, $lte: 50 } });

This query will return Alice (45 credits) and Charlie (50 credits).

Finally, let's use the "not equal to" ($ne) operator to find all students whose grade is not "C".

db.students.find({ grade: { $ne: "C" } });

This will return Alice, Bob, and Charlie, excluding David who has a "C" grade. These operators are fundamental for creating detailed and specific queries in MongoDB.

Match with Regular Expressions

Regular expressions (regex) provide a flexible way to perform powerful text searches. In this step, you will learn how to use regex in your MongoDB queries.

First, let's prepare the data for this step. Clear the collection and insert new documents with more detailed string fields.

db.students.drop();
db.students.insertMany([
  {
    name: "Alice Johnson",
    age: 22,
    major: "Computer Science",
    email: "alice.j@example.com"
  },
  {
    name: "Bob Smith",
    age: 20,
    major: "Mathematics",
    email: "bob.smith@university.edu"
  },
  {
    name: "Charlie Brown",
    age: 25,
    major: "Physics",
    email: "charlie.brown@school.org"
  },
  {
    name: "David Lee",
    age: 19,
    major: "Computer Engineering",
    email: "david.lee@tech.net"
  }
]);

To find all students whose names start with "Alice", you can use the $regex operator with the ^ anchor.

db.students.find({ name: { $regex: "^Alice" } });

This query will return the document for "Alice Johnson". The ^ character signifies the beginning of the string.

To find students with an email address from a specific domain, like example.com, use the $ anchor to match the end of the string.

db.students.find({ email: { $regex: "@example.com$" } });

This will find "Alice Johnson" whose email ends with @example.com.

You can also perform case-insensitive searches. The following query finds any student with "smith" in their name, regardless of case, by using the i option.

db.students.find({ name: { $regex: "smith", $options: "i" } });

This query will match "Bob Smith".

Finally, to find any major that contains the word "Computer", you can use a simple regex pattern without any anchors.

db.students.find({ major: { $regex: "Computer" } });

This will return both "Alice Johnson" (Computer Science) and "David Lee" (Computer Engineering).

Check for Field Existence

Sometimes, documents in a collection have different structures. This step teaches you how to query for documents based on whether a specific field exists or not.

Let's start by inserting a new set of documents where some fields are missing.

db.students.drop();
db.students.insertMany([
  { name: "Alice", age: 22, major: "Computer Science", scholarship: 1000 },
  { name: "Bob", age: 20, major: "Mathematics" },
  { name: "Charlie", age: 25, major: "Physics", internship: "Research Lab" },
  { name: "David", age: 19, contact: { phone: "555-1234" } }
]);

To find all students who have a scholarship field, use the $exists operator with a value of true.

db.students.find({ scholarship: { $exists: true } });

This query will return only the document for Alice, as she is the only one with the scholarship field.

Conversely, to find students who do not have an internship field, set $exists to false.

db.students.find({ internship: { $exists: false } });

This will return the documents for Alice, Bob, and David, as none of them have the internship field.

The $exists operator also works on nested fields. To find a student who has a contact object that contains a phone field, you can use dot notation.

db.students.find({ "contact.phone": { $exists: true } });

This query specifically looks for the phone field inside the contact object and will return the document for David.

Find Null Values

In this final step, you will learn how to query for documents that contain null values. A null value represents the intentional absence of a value.

First, let's set up a collection with documents containing null values and missing fields.

db.students.drop();
db.students.insertMany([
  { name: "Alice", age: 22, email: null },
  { name: "Bob", age: 20, major: "Mathematics" },
  { name: "Charlie", age: null, major: "Physics" },
  { name: "David", contact: { email: null } }
]);

To find documents where a field has a null value, you can query for null directly. The following query finds all documents where the email field is null OR the email field does not exist.

db.students.find({ email: null });

This query will return all four documents: Alice (where email is explicitly null), Bob (where the email field is missing), Charlie (where the email field is missing), and David (where the email field is missing).

To find only the documents where a field is explicitly set to null, you must combine the null check with an $exists check.

db.students.find({ email: { $type: "null" } });

Alternatively, you can use the $type operator. The BSON type for null is 10. This query will only return the document for Alice.

db.students.find({ email: { $type: 10 } });

You can also query for null values within nested documents. The following query finds documents where the email field inside the contact object is null OR where the contact field does not exist.

db.students.find({ "contact.email": null });

This will return all four documents: Alice, Bob, and Charlie (where the contact field is missing), and David (where contact.email is explicitly null). Understanding how to query for null is crucial for handling incomplete or sparse data.

When you are finished, you can exit the mongosh shell by typing exit or pressing Ctrl+D.

Summary

In this lab, you have learned several essential techniques for filtering collections in MongoDB. You practiced applying multiple conditions using the $and and $or operators, which allows for the creation of complex and precise queries. You also explored a range of comparison operators like $gt, $lt, and $ne to refine search criteria. Furthermore, you gained experience with matching text using regular expressions, checking for the existence of fields with $exists, and querying for null values. These skills provide a strong foundation for effective data retrieval and manipulation in MongoDB.