Bird
Raised Fist0
MongoDBquery~20 mins

$inc operator for incrementing in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
MongoDB $inc Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Increment a field using $inc
Given a MongoDB document { _id: 1, score: 10 }, what will be the document after applying { $inc: { score: 5 } } update?
MongoDB
db.collection.updateOne({ _id: 1 }, { $inc: { score: 5 } })
db.collection.findOne({ _id: 1 })
A{ _id: 1, score: 5 }
B{ _id: 1, score: 15 }
C{ _id: 1, score: 10 }
DError: $inc requires a numeric value
Attempts:
2 left
💡 Hint
Think about what adding 5 to 10 results in.
📝 Syntax
intermediate
2:00remaining
Identify the correct $inc syntax
Which of the following update commands correctly increments the count field by 3 in MongoDB?
Adb.collection.updateOne({}, { $inc: count: 3 })
Bdb.collection.updateOne({}, { inc: { count: 3 } })
Cdb.collection.updateOne({}, { $inc: { count: 3 } })
Ddb.collection.updateOne({}, { $inc: { count += 3 } })
Attempts:
2 left
💡 Hint
Remember the $inc operator must be inside an object with the field name as key.
optimization
advanced
2:00remaining
Efficiently increment multiple fields
You want to increment likes by 2 and shares by 1 in a single update. Which update command is the most efficient?
Adb.collection.updateOne({ _id: 1 }, { $inc: { likes: 2 } }); db.collection.updateOne({ _id: 1 }, { $inc: { shares: 1 } })
Bdb.collection.updateOne({ _id: 1 }, { $inc: { likes: 1, shares: 2 } })
Cdb.collection.updateOne({ _id: 1 }, { $set: { likes: likes + 2, shares: shares + 1 } })
Ddb.collection.updateOne({ _id: 1 }, { $inc: { likes: 2, shares: 1 } })
Attempts:
2 left
💡 Hint
Try to do both increments in one command.
🧠 Conceptual
advanced
2:00remaining
Behavior of $inc on non-existing fields
What happens when you use { $inc: { views: 1 } } on a document that does not have the views field?
AThe <code>views</code> field is created with value 1
BThe <code>views</code> field is created with value 0
CThe document remains unchanged
DThe update fails with an error
Attempts:
2 left
💡 Hint
Think about how $inc treats missing fields.
🔧 Debug
expert
2:00remaining
Diagnose the error in $inc usage
A developer runs this update: db.collection.updateOne({ _id: 1 }, { $inc: { score: 'five' } }). What error will MongoDB raise?
AError: The $inc value must be a number
BNo error, score becomes 'five'
CError: Missing $set operator
DError: Invalid query selector
Attempts:
2 left
💡 Hint
Check the type of the value used with $inc.

Practice

(1/5)
1. What does the $inc operator do in MongoDB?
easy
A. It creates a new collection.
B. It increments or decrements the value of a numeric field.
C. It deletes a field from the document.
D. It replaces the entire document with a new one.

Solution

  1. Step 1: Understand the purpose of $inc

    The $inc operator is used to add or subtract a number from an existing numeric field in a document.
  2. Step 2: Compare with other options

    Replacing documents, deleting fields, or creating collections are not functions of $inc.
  3. Final Answer:

    It increments or decrements the value of a numeric field. -> Option B
  4. Quick Check:

    $inc changes numbers by adding/subtracting [OK]
Hint: Remember: $inc changes numbers, not documents [OK]
Common Mistakes:
  • Thinking $inc replaces the whole document
  • Confusing $inc with delete or create operations
  • Assuming $inc works on non-numeric fields
2. Which of the following is the correct syntax to increment the field score by 5 in MongoDB?
easy
A. { $inc: { score: '5' } }
B. { $inc: { score: '+5' } }
C. { $inc: { 'score': 'five' } }
D. { $inc: { score: 5 } }

Solution

  1. Step 1: Check the value type for $inc

    The value to increment must be a number, not a string or word.
  2. Step 2: Validate syntax correctness

    { $inc: { score: 5 } } uses a number 5 correctly. { $inc: { score: '5' } } uses a string '5', which is invalid. { $inc: { 'score': 'five' } } uses a word 'five', invalid. { $inc: { score: '+5' } } uses a string '+5', invalid.
  3. Final Answer:

    { $inc: { score: 5 } } -> Option D
  4. Quick Check:

    Use number values with $inc [OK]
Hint: Use numeric values without quotes for $inc [OK]
Common Mistakes:
  • Using strings instead of numbers for increment values
  • Adding plus sign (+) inside JSON value
  • Using words instead of numeric literals
3. Given a document { _id: 1, count: 10 }, what will be the document after running db.collection.updateOne({ _id: 1 }, { $inc: { count: -3 } })?
medium
A. { _id: 1, count: -3 }
B. { _id: 1, count: 13 }
C. { _id: 1, count: 7 }
D. { _id: 1, count: 10 }

Solution

  1. Step 1: Understand the initial document

    The document has count equal to 10.
  2. Step 2: Apply the $inc operation with -3

    Subtract 3 from 10: 10 - 3 = 7.
  3. Final Answer:

    { _id: 1, count: 7 } -> Option C
  4. Quick Check:

    10 + (-3) = 7 [OK]
Hint: Subtract by using negative numbers with $inc [OK]
Common Mistakes:
  • Adding instead of subtracting when using negative values
  • Replacing the whole document instead of updating
  • Assuming $inc only increments, not decrements
4. You run this update: db.collection.updateOne({ _id: 2 }, { $inc: { visits: 1 } }) but get an error. What is the most likely cause?
medium
A. The visits field is a string, not a number.
B. The _id field is missing in the query.
C. The $inc operator cannot increment by 1.
D. The collection does not exist.

Solution

  1. Step 1: Understand $inc requirements

    $inc only works on numeric fields. If visits is a string, it causes an error.
  2. Step 2: Check other options

    Missing _id would not cause this error if the document exists. $inc can increment by 1. Collection existence error would be different.
  3. Final Answer:

    The visits field is a string, not a number. -> Option A
  4. Quick Check:

    $inc needs numeric fields [OK]
Hint: Ensure field is numeric before using $inc [OK]
Common Mistakes:
  • Trying to increment string fields
  • Assuming $inc works on any data type
  • Ignoring error messages about field types
5. You want to increment the likes field by 1 for all documents where likes does not exist yet. Which update command will correctly do this without errors?
hard
A. db.collection.updateMany({ likes: { $exists: false } }, { $inc: { likes: 1 } })
B. db.collection.updateMany({}, { $inc: { likes: 1 } })
C. db.collection.updateMany({ likes: null }, { $inc: { likes: 1 } })
D. db.collection.updateMany({}, { $set: { likes: 1 } })

Solution

  1. Step 1: Identify documents without likes

    Use the query { likes: { $exists: false } } to find documents missing the likes field.
  2. Step 2: Use $inc to add 1 to likes

    $inc will create the field with value 1 if it does not exist, so this safely increments missing fields.
  3. Step 3: Check other options

    db.collection.updateMany({}, { $inc: { likes: 1 } }) increments all documents, including those with likes. db.collection.updateMany({ likes: null }, { $inc: { likes: 1 } }) matches documents where likes is null, not missing. db.collection.updateMany({}, { $set: { likes: 1 } }) sets likes to 1, not increments.
  4. Final Answer:

    db.collection.updateMany({ likes: { $exists: false } }, { $inc: { likes: 1 } }) -> Option A
  5. Quick Check:

    Increment missing fields with $exists: false filter [OK]
Hint: Filter missing fields with $exists: false before $inc [OK]
Common Mistakes:
  • Incrementing all documents without filtering
  • Using $set instead of $inc
  • Filtering with null instead of $exists: false