0
0
MongoDBquery~20 mins

$ne for not equal in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $ne Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents where age is not 30
Given a collection users with documents containing an age field, which query returns all users whose age is not 30?
MongoDB
db.users.find({ age: { $ne: 30 } })
AReturns users with age greater than 30
BReturns only users with age 30
CReturns all users regardless of age
DReturns all users except those with age 30
Attempts:
2 left
💡 Hint
Use $ne to find values not equal to a specific number.
🧠 Conceptual
intermediate
2:00remaining
Understanding $ne with null values
What does the query db.collection.find({ field: { $ne: null } }) return?
ADocuments where <code>field</code> is null
BDocuments where <code>field</code> does not exist or exists and is not null
CDocuments where <code>field</code> does not exist
DDocuments where <code>field</code> is either null or does not exist
Attempts:
2 left
💡 Hint
Think about how MongoDB treats null and missing fields.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $ne usage
Which of the following queries has a syntax error?
Adb.items.find({ price: { $ne: null } })
Bdb.items.find({ price: { $ne: 100 } })
Cdb.items.find({ price: $ne: 100 })
Ddb.items.find({ price: { $ne: '100' } })
Attempts:
2 left
💡 Hint
Check the correct way to use operators inside query objects.
optimization
advanced
2:00remaining
Optimizing queries with $ne
Which query is more efficient to find documents where status is not 'active'?
Adb.records.find({ status: { $ne: 'active' } })
Bdb.records.find({ status: { $not: { $eq: 'active' } } })
Cdb.records.find({ status: { $nin: ['active'] } })
Ddb.records.find({ status: { $exists: true, $ne: 'active' } })
Attempts:
2 left
💡 Hint
Consider the simplest operator that directly expresses the condition.
🔧 Debug
expert
3:00remaining
Why does this $ne query return unexpected results?
Consider the query db.products.find({ category: { $ne: 'electronics' } }). It returns documents where category is missing. Why does this happen?
ABecause $ne matches documents where the field is not equal or the field does not exist
BBecause $ne only matches documents where the field exists and is not equal
CBecause MongoDB treats missing fields as null and excludes them
DBecause the query syntax is incorrect and matches all documents
Attempts:
2 left
💡 Hint
Think about how MongoDB treats missing fields with $ne.