Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I use MongoDB to find all documents which have a field, regardless of the value of that field?
To use MongoDB to find all documents which have a field, regardless of the value of that field, use the $exists operator. This operator returns documents where the specified field exists, even if its value is null or empty.
Syntax
db.collectionName.find({
fieldName: { $exists: true }
});
Sample Data
Let us create a collection with sample documents ?
db.students.insertMany([
{ "StudentName": "John", "StudentAge": null },
{ "StudentName": "Larry", "StudentAge": null },
{ "StudentName": "Chris", "StudentAge": "" },
{ "StudentName": "Robert", "StudentAge": "" },
{ "StudentName": "Mike" }
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5c9d1d60a629b87623db1b22"),
ObjectId("5c9d1d70a629b87623db1b23"),
ObjectId("5c9d1d7ba629b87623db1b24"),
ObjectId("5c9d1d81a629b87623db1b25"),
ObjectId("5c9d1d91a629b87623db1b26")
]
}
Display all documents from the collection ?
db.students.find();
{
"_id": ObjectId("5c9d1d60a629b87623db1b22"),
"StudentName": "John",
"StudentAge": null
}
{
"_id": ObjectId("5c9d1d70a629b87623db1b23"),
"StudentName": "Larry",
"StudentAge": null
}
{
"_id": ObjectId("5c9d1d7ba629b87623db1b24"),
"StudentName": "Chris",
"StudentAge": ""
}
{
"_id": ObjectId("5c9d1d81a629b87623db1b25"),
"StudentName": "Robert",
"StudentAge": ""
}
{
"_id": ObjectId("5c9d1d91a629b87623db1b26"),
"StudentName": "Mike"
}
Example: Find Documents with StudentAge Field
Find all documents that have the StudentAge field, regardless of its value ?
db.students.find({
StudentAge: { $exists: true }
});
{
"_id": ObjectId("5c9d1d60a629b87623db1b22"),
"StudentName": "John",
"StudentAge": null
}
{
"_id": ObjectId("5c9d1d70a629b87623db1b23"),
"StudentName": "Larry",
"StudentAge": null
}
{
"_id": ObjectId("5c9d1d7ba629b87623db1b24"),
"StudentName": "Chris",
"StudentAge": ""
}
{
"_id": ObjectId("5c9d1d81a629b87623db1b25"),
"StudentName": "Robert",
"StudentAge": ""
}
Exclude Fields from Result
Find documents with StudentAge field but exclude StudentName from the result ?
db.students.find(
{ StudentAge: { $exists: true } },
{ StudentName: 0 }
);
{
"_id": ObjectId("5c9d1d60a629b87623db1b22"),
"StudentAge": null
}
{
"_id": ObjectId("5c9d1d70a629b87623db1b23"),
"StudentAge": null
}
{
"_id": ObjectId("5c9d1d7ba629b87623db1b24"),
"StudentAge": ""
}
{
"_id": ObjectId("5c9d1d81a629b87623db1b25"),
"StudentAge": ""
}
Key Points
-
$exists: truematches documents where the field exists, includingnulland empty values. -
$exists: falsematches documents where the field does not exist. - Field projection can be used to include or exclude specific fields from the result.
Conclusion
The $exists operator is essential for finding documents based on field presence rather than field values. It returns all documents containing the specified field, regardless of whether the value is null, empty, or has actual data.
