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
Selected Reading
The collection.find() always returns all fields with MongoDB?
By default, collection.find() returns all fields from matching documents in MongoDB. However, you can use field projection to specify which fields to include or exclude in the result set.
Syntax
// Include specific fields (projection: 1)
db.collection.find({}, {"fieldName": 1});
// Exclude specific fields (projection: 0)
db.collection.find({}, {"fieldName": 0});
// Return all fields (default behavior)
db.collection.find();
Sample Data
db.returnFieldInFindDemo.insertMany([
{
"StudentName": "John",
"StudentAge": 23,
"TechnicalSubject": ["MongoDB", "MySQL"]
},
{
"StudentName": "Mike",
"StudentAge": 24,
"TechnicalSubject": ["Java", "Python"]
},
{
"StudentName": "Sam",
"StudentAge": 22,
"TechnicalSubject": ["C", "C++"]
},
{
"StudentName": "Carol",
"StudentAge": 20,
"TechnicalSubject": ["DataStructure", "Algorithm"]
}
]);
Case 1: Include Specific Fields (Projection: 1)
To return only the TechnicalSubject field ?
db.returnFieldInFindDemo.find({}, {"TechnicalSubject": 1});
{
"_id": ObjectId("5c8ebfe72f684a30fbdfd566"),
"TechnicalSubject": ["MongoDB", "MySQL"]
}
{
"_id": ObjectId("5c8ebffd2f684a30fbdfd567"),
"TechnicalSubject": ["Java", "Python"]
}
{
"_id": ObjectId("5c8ec00f2f684a30fbdfd568"),
"TechnicalSubject": ["C", "C++"]
}
{
"_id": ObjectId("5c8ec0292f684a30fbdfd569"),
"TechnicalSubject": ["DataStructure", "Algorithm"]
}
Case 2: Exclude Specific Fields (Projection: 0)
To return all fields except TechnicalSubject ?
db.returnFieldInFindDemo.find({}, {"TechnicalSubject": 0});
{
"_id": ObjectId("5c8ebfe72f684a30fbdfd566"),
"StudentName": "John",
"StudentAge": 23
}
{
"_id": ObjectId("5c8ebffd2f684a30fbdfd567"),
"StudentName": "Mike",
"StudentAge": 24
}
{
"_id": ObjectId("5c8ec00f2f684a30fbdfd568"),
"StudentName": "Sam",
"StudentAge": 22
}
{
"_id": ObjectId("5c8ec0292f684a30fbdfd569"),
"StudentName": "Carol",
"StudentAge": 20
}
Case 3: Return All Fields (Default)
Using find() without projection returns all fields ?
db.returnFieldInFindDemo.find();
{"_id": ObjectId("5c8ebfe72f684a30fbdfd566"), "StudentName": "John", "StudentAge": 23, "TechnicalSubject": ["MongoDB", "MySQL"]}
{"_id": ObjectId("5c8ebffd2f684a30fbdfd567"), "StudentName": "Mike", "StudentAge": 24, "TechnicalSubject": ["Java", "Python"]}
{"_id": ObjectId("5c8ec00f2f684a30fbdfd568"), "StudentName": "Sam", "StudentAge": 22, "TechnicalSubject": ["C", "C++"]}
{"_id": ObjectId("5c8ec0292f684a30fbdfd569"), "StudentName": "Carol", "StudentAge": 20, "TechnicalSubject": ["DataStructure", "Algorithm"]}
Key Points
- Setting field to 1 includes only that field (plus
_idby default) - Setting field to 0 excludes that field, returns all others
- Cannot mix inclusion and exclusion projections (except with
_id) - Use
{"_id": 0}to exclude the_idfield when needed
Conclusion
MongoDB's find() returns all fields by default, but field projection allows you to control which fields are included or excluded. Use projection to optimize queries and reduce network traffic by retrieving only necessary data.
Advertisements
