0
0
MongoDBquery~30 mins

Regex queries in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Regex Queries in MongoDB
📖 Scenario: You work at a small bookstore that stores book information in a MongoDB database. You want to find books by searching for patterns in their titles using regular expressions.
🎯 Goal: Build a MongoDB query that uses regex to find books with titles matching specific patterns.
📋 What You'll Learn
Create a collection called books with sample book documents
Add a regex pattern variable to search titles
Write a MongoDB query using $regex to find matching books
Complete the query with options to make the search case-insensitive
💡 Why This Matters
🌍 Real World
Bookstores and libraries often need to search for books by partial title matches or patterns. Regex queries in MongoDB help find these books efficiently.
💼 Career
Knowing how to use regex queries in MongoDB is useful for database developers and data analysts who work with flexible text search requirements.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a MongoDB collection called books and insert these exact documents: { title: "The Great Gatsby", author: "F. Scott Fitzgerald" }, { title: "Great Expectations", author: "Charles Dickens" }, and { title: "Gone with the Wind", author: "Margaret Mitchell" }.
MongoDB
Need a hint?

Use insertMany on db.books with an array of the three book objects.

2
Add a regex pattern variable for searching titles
Create a variable called pattern and set it to the regex string "Great" to search for titles containing the word "Great".
MongoDB
Need a hint?

Use const pattern = "Great" to define the regex string.

3
Write a MongoDB query using $regex to find matching books
Write a MongoDB query that finds all documents in books where the title field matches the regex pattern stored in pattern. Use $regex with the variable pattern.
MongoDB
Need a hint?

Use db.books.find({ title: { $regex: pattern } }) to query with regex.

4
Complete the query with case-insensitive option
Modify the query to add the $options field with value "i" to make the regex search case-insensitive.
MongoDB
Need a hint?

Add $options: "i" inside the title query object to ignore case.