Filter MongoDB Collections

MongoDBMongoDBBeginner
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 regular expressions, checking field existence, and finding null values. These skills are essential for querying and manipulating data in MongoDB, allowing you to extract precise and relevant information from your database. The step-by-step instructions provide practical examples and explanations to help you master the fundamentals of MongoDB querying.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL mongodb(("`MongoDB`")) -.-> mongodb/QueryOperationsGroup(["`Query Operations`"]) mongodb(("`MongoDB`")) -.-> mongodb/DataTypesGroup(["`Data Types`"]) mongodb(("`MongoDB`")) -.-> mongodb/ErrorHandlingGroup(["`Error Handling`"]) mongodb/QueryOperationsGroup -.-> mongodb/find_documents("`Find Documents`") mongodb/QueryOperationsGroup -.-> mongodb/query_with_conditions("`Query with Conditions`") mongodb/QueryOperationsGroup -.-> mongodb/sort_documents("`Sort Documents`") mongodb/QueryOperationsGroup -.-> mongodb/project_fields("`Project Fields`") mongodb/DataTypesGroup -.-> mongodb/use_numeric_data_types("`Use Numeric Data Types`") mongodb/DataTypesGroup -.-> mongodb/use_string_data_types("`Use String Data Types`") mongodb/ErrorHandlingGroup -.-> mongodb/handle_write_errors("`Handle Write Errors`") subgraph Lab Skills mongodb/find_documents -.-> lab-421806{{"`Filter MongoDB Collections`"}} mongodb/query_with_conditions -.-> lab-421806{{"`Filter MongoDB Collections`"}} mongodb/sort_documents -.-> lab-421806{{"`Filter MongoDB Collections`"}} mongodb/project_fields -.-> lab-421806{{"`Filter MongoDB Collections`"}} mongodb/use_numeric_data_types -.-> lab-421806{{"`Filter MongoDB Collections`"}} mongodb/use_string_data_types -.-> lab-421806{{"`Filter MongoDB Collections`"}} mongodb/handle_write_errors -.-> lab-421806{{"`Filter MongoDB Collections`"}} end

Apply Multiple Conditions

In this step, you'll learn how to apply multiple conditions when querying MongoDB collections. Multiple conditions allow you to create more precise and complex queries to filter your data effectively.

First, open a terminal and launch the MongoDB shell:

mongosh

Now, let's create a sample collection of students to demonstrate multiple condition queries:

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

Let's explore how to apply multiple conditions using the $and operator. This allows us to specify multiple conditions that must all be true:

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

This query will return students who are 20 years or older AND have an "A" grade. Let's break down the conditions:

  • $gte means "greater than or equal to"
  • $and ensures both conditions must be met

You should see output similar to:

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

We can also use the $or operator to find documents that match at least one condition:

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

This query will return students who are either in Computer Science OR under 21 years old.

  • $lt means "less than"

The result will include students like David (under 21) and Alice and David (Computer Science majors).

Use Comparison Operators

In this step, you'll learn about MongoDB's comparison operators that help you create more sophisticated queries to filter data based on different conditions.

First, ensure you're in the MongoDB shell:

mongosh

Let's continue using the school_database from the previous step. If you've closed the shell, recreate the students collection:

use school_database

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

MongoDB provides powerful comparison operators to help you filter data precisely. Let's explore some key operators:

  1. Greater Than ($gt) and Less Than ($lt):
db.students.find({ age: { $gt: 20, $lt: 25 } });

This query finds students older than 20 but younger than 25. In our example, this would return Alice.

  1. Greater Than or Equal To ($gte) and Less Than or Equal To ($lte):
db.students.find({ credits: { $gte: 40, $lte: 50 } });

This finds students with 40 to 50 credits (inclusive). This will return Alice and Charlie.

  1. Not Equal To ($ne):
db.students.find({ grade: { $ne: "C" } });

This returns students whose grade is not "C", which would be Alice, Bob, and Charlie.

Let's break down these operators:

  • $gt: Greater than
  • $lt: Less than
  • $gte: Greater than or equal to
  • $lte: Less than or equal to
  • $ne: Not equal to

Each query demonstrates a different way to filter data precisely using comparison operators.

Match Regular Expressions

In this step, you'll learn how to use regular expressions (regex) in MongoDB to perform flexible and powerful text searches in your collections.

First, ensure you're in the MongoDB shell:

mongosh

Let's continue using the school_database and add some more diverse data:

use school_database

db.students.insertMany([
    { name: "Alice Johnson", age: 22, major: "Computer Science", email: "[email protected]" },
    { name: "Bob Smith", age: 20, major: "Mathematics", email: "[email protected]" },
    { name: "Charlie Brown", age: 25, major: "Physics", email: "[email protected]" },
    { name: "David Lee", age: 19, major: "Computer Engineering", email: "[email protected]" }
])

Regular expressions in MongoDB allow you to perform complex text searches. Let's explore different regex patterns:

  1. Find names starting with a specific letter:
db.students.find({ name: { $regex: "^A" } });

This query finds students whose names start with "A". The ^ symbol means "starts with".

  1. Find emails from a specific domain:
db.students.find({ email: { $regex: "@example.com$" } });

This query finds students with email addresses ending in "@example.com". The $ symbol means "ends with".

  1. Case-insensitive regex search:
db.students.find({ name: { $regex: "johnson", $options: "i" } });

This finds names containing "johnson" regardless of case. The $options: "i" makes the search case-insensitive.

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

This finds students with "Computer" anywhere in their major.

Regex patterns provide powerful ways to search and filter data:

  • ^: Starts with
  • $: Ends with
  • $options: "i": Case-insensitive matching
  • Partial matches don't require special symbols

Check Field Existence

In this step, you'll learn how to check for the existence of fields in MongoDB documents using powerful operators that help you filter collections based on field presence.

First, ensure you're in the MongoDB shell:

mongosh

Let's create a collection with documents having different structures to demonstrate field existence checks:

use school_database

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

MongoDB provides the $exists operator to check field presence:

  1. Find documents with a specific field:
db.students.find({ scholarship: { $exists: true } });

This query returns students who have a scholarship field. In our example, this will return Alice's document.

  1. Find documents without a specific field:
db.students.find({ internship: { $exists: false } });

This query returns students without an internship field, which includes Alice, Bob, and David.

  1. Combine $exists with nested field checks:
db.students.find({
  contact: { $exists: true },
  "contact.phone": { $exists: true }
});

This finds documents with a contact field that also has a phone sub-field, which matches David's document.

Key points about field existence:

  • $exists: true checks if a field is present
  • $exists: false checks if a field is absent
  • You can create complex queries checking nested field existence

Find Null Values

In this step, you'll learn how to find and work with null values in MongoDB, understanding the difference between null, undefined, and missing fields.

First, ensure you're in the MongoDB shell:

mongosh

Let's create a collection with documents containing null and undefined values:

use school_database

db.students.insertMany([
    { name: "Alice Johnson", age: 22, email: null },
    { name: "Bob Smith", age: null, major: "Mathematics" },
    { name: "Charlie Brown", age: 25, phone: undefined },
    { name: "David Lee", contact: { email: null } }
])

MongoDB provides specific ways to query null and undefined values:

  1. Find documents with null values:
db.students.find({ email: null });

This query returns documents where the email field is explicitly set to null. In our example, this will return Alice's document.

  1. Find documents with null fields:
db.students.find({ age: null });

This finds documents where the age field is specifically null. This will return Bob's document.

  1. Check for null in nested documents:
db.students.find({ "contact.email": null });

This finds documents with null email in a nested contact object, which matches David's document.

  1. Distinguish between null and missing fields:
db.students.find({
  $and: [{ phone: null }, { phone: { $exists: true } }]
});

This query finds documents where the phone field exists and is null. In our example, this won't return any documents.

Key points about null values:

  • null represents an intentional absence of any object value
  • Different from undefined which means a variable has not been assigned
  • Use $exists to check field presence
  • Careful with nested document queries

Summary

In this lab, you learned how to apply multiple conditions when querying MongoDB collections using the $and and $or operators. This allows you to create more precise and complex queries to filter your data effectively. You also explored various comparison operators such as $gte (greater than or equal to) and $lt (less than) to refine your search criteria. Additionally, you discovered how to match regular expressions, check field existence, and find null values in your MongoDB collections, providing you with powerful tools to effectively filter and retrieve the data you need.

Other MongoDB Tutorials you may like