Introduction
MongoDB element operators are used to check whether a field exists in a document or determine the BSON data type stored in a field. The two main element operators are $exists and $type. They are useful when working with documents that may have different fields or data types. In this chapter, you will practice these operators with simple, real-world MongoDB queries. MongoDB Element operators practice questions with solutions to help you understand the concepts.
Q1. Find Documents Where the Email Field Exists
Problem Statement
Find all students who have an email field in their document.
MongoDB Command / Query
db.students.find({
email: { $exists: true }
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python",
email: "rahul@example.com"
},
{
name: "Priya",
age: 15,
course: "Java",
email: "priya@example.com"
}
]
Explanation
$exists: true finds documents where the specified field exists.
Q2. Find Students Without an Email Field
Problem Statement
Find all students whose documents do not contain an email field.
MongoDB Command / Query
db.students.find({
email: { $exists: false }
})
Expected Output
[
{
name: "Aman",
age: 18,
course: "C++"
},
{
name: "Neha",
age: 17,
course: "JavaScript"
}
]
Explanation
$exists: false finds documents where the field is missing.
Note: A field with the value
nullstill exists.$exists: falsespecifically checks whether the field is absent.
Q3. Find Documents Where Age Is Stored as a Number
Problem Statement
Find students whose age field is stored as a numeric BSON type.
MongoDB Command / Query
db.students.find({
age: { $type: "number" }
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
},
{
name: "Priya",
age: 15,
course: "Java"
}
]
Explanation
$type: "number" finds documents where the field contains a numeric BSON value.
MongoDB’s "number" alias matches numeric BSON types such as integers and doubles.
Q4. Find Documents Where Name Is a String
Problem Statement
Find all students where the name field is stored as a string.
MongoDB Command / Query
db.students.find({
name: { $type: "string" }
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
},
{
name: "Priya",
age: 15,
course: "Java"
}
]
Explanation
$type: "string" checks whether the value of name is a BSON string.
Q5. Find Documents Where Skills Are Stored as an Array
Problem Statement
Find students whose skills field contains an array.
MongoDB Command / Query
db.students.find({
skills: { $type: "array" }
})
Expected Output
[
{
name: "Rahul",
skills: ["Python", "MongoDB"]
},
{
name: "Neha",
skills: ["HTML", "CSS", "JavaScript"]
}
]
Explanation
$type: "array" finds documents where the specified field is stored as an array.
Q6. Find Documents Where Address Is an Object
Problem Statement
Find students whose address field contains an embedded document/object.
MongoDB Command / Query
db.students.find({
address: { $type: "object" }
})
Expected Output
[
{
name: "Rahul",
address: {
city: "Delhi",
country: "India"
}
}
]
Explanation
MongoDB stores embedded documents as the BSON object type.
Q7. Find Documents Where CreatedAt Is a Date
Problem Statement
Find all documents where the createdAt field contains a BSON Date value.
MongoDB Command / Query
db.students.find({
createdAt: { $type: "date" }
})
Expected Output
[
{
name: "Rahul",
createdAt: ISODate("2026-09-14T08:00:00.000Z")
}
]
Explanation
$type: "date" finds documents where createdAt is stored as a MongoDB BSON Date.
Q8. Find Documents Where the Active Field Is Boolean
Problem Statement
Find all students where the active field contains a Boolean value.
MongoDB Command / Query
db.students.find({
active: { $type: "bool" }
})
Expected Output
[
{
name: "Rahul",
active: true
},
{
name: "Priya",
active: false
}
]
Explanation
$type: "bool" checks whether the field contains a Boolean value such as true or false.
Q9. Find Documents Where the ID Is an ObjectId
Problem Statement
Find documents where the _id field is stored as an ObjectId.
MongoDB Command / Query
db.students.find({
_id: { $type: "objectId" }
})
Expected Output
[
{
_id: ObjectId("68c6a1234567890123456789"),
name: "Rahul",
age: 16
},
{
_id: ObjectId("68c6a9876543210987654321"),
name: "Priya",
age: 15
}
]
Explanation
MongoDB commonly creates _id values using the BSON ObjectId type.
Q10. Find Documents Where Phone Exists and Is a String
Problem Statement
Find students whose phone field exists and is stored as a string.
MongoDB Command / Query
db.students.find({
$and: [
{ phone: { $exists: true } },
{ phone: { $type: "string" } }
]
})
Expected Output
[
{
name: "Rahul",
phone: "9876543210"
},
{
name: "Neha",
phone: "9123456780"
}
]
Explanation
This query combines two conditions:
phone field exists
AND
phone is stored as a string
This is useful when you want to check both the presence and data type of a field.
Key Takeaways
- MongoDB has two main element operators:
$existsand$type. $exists: truechecks whether a field exists.$exists: falsechecks whether a field is missing.$typechecks the BSON data type of a field.$type: "string"checks for string values.$type: "number"checks for numeric BSON values.$type: "array"checks for arrays.$type: "object"checks for embedded documents.$type: "date"checks for BSON Date values.$type: "bool"checks for Boolean values.$type: "objectId"checks for ObjectId values.$existsdoes not mean the field must have a non-null value; it only checks whether the field is present.
FAQs
1. What are element operators in MongoDB?
Element operators are MongoDB query operators used to check whether a field exists and what BSON data type it contains. The main element operators are $exists and $type.
2. What is the use of $exists in MongoDB?
$exists checks whether a particular field is present in a document.
db.students.find({
email: { $exists: true }
})
3. What is the difference between $exists: true and $exists: false?
$exists: true finds documents where the field is present, while $exists: false finds documents where the field is missing.
4. What does $type do in MongoDB?
$type checks the BSON data type of a field.
For example:
db.students.find({
age: { $type: "number" }
})
finds documents where age contains a numeric value.
5. Can $type check whether a field contains an array?
Yes. You can use:
db.students.find({
skills: { $type: "array" }
})
This finds documents where skills is an array.
6. Does $exists: false find fields with a null value?
No. $exists: false looks for documents where the field is missing. A field with null as its value still exists.
7. What are common BSON types used with $type?
Common BSON type aliases include:
"string""number""bool""array""object""date""objectId""null"
These can be used with $type to check the type of stored data.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
