Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Working with Boolean and Null Types in MongoDB
📖 Scenario: You are managing a small online store database. You want to keep track of whether products are in stock and if they have any special discount offers. Some products might not have discount information yet.
🎯 Goal: Create a MongoDB collection with product documents that use Boolean and null types correctly. Then, write queries to find products based on these Boolean and null values.
📋 What You'll Learn
Create a collection named products with documents containing name, inStock (Boolean), and discount (Boolean or null).
Insert exactly three products with specified values for inStock and discount.
Write a query to find all products that are in stock (inStock is true).
Write a query to find all products where discount is null (no discount information).
💡 Why This Matters
🌍 Real World
Online stores often track product availability and discount status using Boolean and null values in their databases.
💼 Career
Understanding how to use Boolean and null types in MongoDB is essential for database roles that manage product inventories and sales data.
Progress0 / 4 steps
1
Create the products collection with three product documents
Create a MongoDB collection called products and insert these three documents exactly: { name: "Laptop", inStock: true, discount: false }, { name: "Headphones", inStock: false, discount: null }, { name: "Mouse", inStock: true, discount: true }.
MongoDB
Hint
Use db.products.insertMany() with an array of objects for the three products.
2
Create a query variable to find products that are in stock
Create a variable called inStockQuery that holds a MongoDB query object to find products where inStock is true.
MongoDB
Hint
Set inStockQuery to an object with inStock: true.
3
Create a query variable to find products with null discount
Create a variable called nullDiscountQuery that holds a MongoDB query object to find products where discount is null.
MongoDB
Hint
Set nullDiscountQuery to an object with discount: null.
4
Write queries to find products using the query variables
Write two queries using db.products.find() with the variables inStockQuery and nullDiscountQuery to find products in stock and products with null discount, respectively. Assign the results to inStockProducts and nullDiscountProducts.
MongoDB
Hint
Use db.products.find() with the query variables and assign to the specified variables.
Practice
(1/5)
1. What does the Boolean type represent in MongoDB?
easy
A. Text strings
B. Numbers only
C. True or false values
D. Dates and times
Solution
Step 1: Understand Boolean type meaning
Boolean type stores only two possible values: true or false.
Step 2: Compare with other data types
Numbers, strings, and dates are different types, not Boolean.
Final Answer:
True or false values -> Option C
Quick Check:
Boolean = true/false [OK]
Hint: Boolean means true or false only [OK]
Common Mistakes:
Confusing Boolean with numbers
Thinking Boolean stores text
Mixing Boolean with date types
2. Which of the following is the correct way to store a null value in a MongoDB document?
easy
A. { "field": "null" }
B. { "field": false }
C. { "field": 0 }
D. { "field": null }
Solution
Step 1: Identify null value syntax
In MongoDB, null is stored as the keyword null without quotes.
Step 2: Check other options
"null" is a string, 0 is a number, false is Boolean, so they are incorrect.
Final Answer:
{ "field": null } -> Option D
Quick Check:
Null stored as null keyword [OK]
Hint: Use null without quotes for null values [OK]