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
$size Operator for Array Length in MongoDB
📖 Scenario: You are managing a database for a book club. Each book document stores the title and a list of members who have read it.
🎯 Goal: Build a MongoDB query that finds books with exactly 3 readers using the $size operator.
📋 What You'll Learn
Create a collection named books with documents containing title and readers array fields.
Insert 3 book documents with specific titles and readers arrays.
Write a query using the $size operator to find books with exactly 3 readers.
Add a projection to show only the title field in the query result.
💡 Why This Matters
🌍 Real World
Managing book club data where you want to find books read by a specific number of members.
💼 Career
Filtering documents by array length is common in MongoDB for tasks like user activity tracking, inventory management, and social media data analysis.
Progress0 / 4 steps
1
Create the books collection with 3 documents
Insert 3 documents into the books collection with these exact entries: { title: "The Hobbit", readers: ["Alice", "Bob", "Charlie"] }, { title: "1984", readers: ["Dave", "Eve"] }, and { title: "Dune", readers: ["Frank", "Grace", "Heidi"] }.
MongoDB
Hint
Use db.books.insertMany([...]) to add multiple documents at once.
2
Set the target array length to 3
Create a variable called targetLength and set it to 3 to represent the number of readers to filter by.
MongoDB
Hint
Use const targetLength = 3 to store the number 3.
3
Write a query using $size to find books with 3 readers
Write a MongoDB query using db.books.find() with a filter that uses { readers: { $size: targetLength } } to find books where the readers array length is exactly 3.
MongoDB
Hint
Use { readers: { $size: targetLength } } inside find() to filter by array length.
4
Add a projection to show only the title field
Modify the query to include a projection that returns only the title field by adding { title: 1, _id: 0 } as the second argument to db.books.find().
MongoDB
Hint
Add { title: 1, _id: 0 } as the second argument to find() to show only titles.
Practice
(1/5)
1. What does the $size operator do in MongoDB?
easy
A. Counts the number of elements in an array
B. Calculates the sum of numbers in an array
C. Finds the largest number in an array
D. Sorts the elements of an array
Solution
Step 1: Understand the purpose of $size
The $size operator is used to count how many elements are inside an array field in a MongoDB document.
Step 2: Compare with other options
Other options describe different operations like sum, max, or sort, which are not what $size does.
Final Answer:
Counts the number of elements in an array -> Option A
Quick Check:
$size = count array elements [OK]
Hint: Remember: $size counts array items, not values [OK]
Common Mistakes:
Confusing $size with sum or max functions
Thinking $size sorts arrays
Using $size on non-array fields
2. Which of the following is the correct syntax to use $size in a MongoDB aggregation pipeline to add a field itemCount that counts elements in the items array?
Step 1: Identify correct operator usage in aggregation
The $size operator is used inside an expression to count array elements. It must be inside a stage like $addFields or $project with the array field referenced as "$items".