In this MongoDB tutorial, we are going to learn about MongoDB return specific fields with a few examples. We will cover this by executing various operations with examples. The following index topics we are going to cover in this tutorial:
- MongoDB query return specific fields
- MongoDB return specific fields with any value
- MongoDB return specific fields that exists
- MongoDB return specific field not exists
- MongoDB return specific fields equal
- MongoDB return specific field empty
- MongoDB return specific field regex
- MongoDB return only a specific field
- MongoDB aggregate return specific field
- MongoDB find and return specific field
- MongoDB return specific fields of documents
- MongoDB return specific fields greater than
- MongoDB lookup return specific fields
- MongoDB return a specific field not null
- MongoDB return specific fields not equal
- MongoDB return a specific field max value
- MongoDB return specific fields using python
MongoDB query return specific fields
MongoDB provides the find() method that is used to select documents in a collection or view and returns a cursor to the selected documents.
db.collection.find( query, projection )
Parameter | Type | Description |
---|---|---|
query | document | Optional, It is used to return all documents in a collection, skip this parameter or pass an empty document ({} ). |
projection | document | Optional, Here we define the fields to return in the documents that match the query filter. |
Returns: A cursor to the documents that match the query
criteria. When the find( ) method “returns documents,” It returns a cursor to the documents.
Read: Current date in MongoDB
MongoDB return specific fields with any value
In this topic, you will learn to return specific fields or documents from the collection with any value. Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the detail collection.
db.detail.find().pretty()
{
"_id" : 1,
"Name" : "Larry",
"Age" : 23,
"Date" : ISODate("2021-12-07T09:55:36.608Z"),
"Country" : "United States of America"
}
{
"_id" : 2,
"Name" : "Chris",
"Age" : 26,
"Date" : ISODate("2021-12-07T09:55:36.608Z"),
"Country" : "Canada"
}
{
"_id" : 3,
"Name" : "Robert",
"Age" : 28,
"Date" : ISODate("2021-12-07T09:55:36.608Z"),
"Country" : "Australia"
}
Now, we will apply the below query to return specific fields with any value.
db.detail.find({ Country: "United States of America" })
Here, we have defined the Country, the United States of America in the find( ) method this will return all the documents as per the defined field.

Also, check: $in MongoDB Example
MongoDB return specific fields that exists
In this topic, you will learn to return specific field that exists in the document of the collection. For that, we will use the $exists operator that matches the document that contains the field also including documents where the field value is null.
Syntax:
{ field: { $exists : <boolean> } }
Here, when <boolean> is true then matches the documents that contain the field and if it is false then the query returns only the documents that do not contain the field.
Example:
The following documents were inserted into the records collections.
db.records.find()
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbf9"), "a" : 5, "b" : 5, "c" : null }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfa"), "a" : 3, "b" : null, "c" : 8 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfb"), "a" : null, "b" : 3, "c" : 9 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfc"), "a" : 1, "b" : 2, "c" : 3 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfd"), "a" : 2, "c" : 5 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfe"), "a" : 3, "b" : 2 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbff"), "a" : 4 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bc00"), "b" : 2, "c" : 4 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bc01"), "b" : 2 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bc02"), "c" : 6 }
We will apply the below query to return specific fields that exist.
db.records.find( { b: { $exists: true } } )
Here, we have used the $exists operator where true means the results consist of those documents that contain the field b, including the document whose field b contains a null value.
Output:

Read: Display MongoDB data in HTML
MongoDB return specific field not exists
In this topic, you will learn to return the specific field that does not exist in the document of a collection. For that, we will use the $exists operator that matches the document that contains the field also including documents where the field value is null.
Syntax:
{ field: { $exists : <boolean> } }
Here, when <boolean> is true then matches the documents that contain the field and if it is false then the query returns only the documents that do not contain the field.
Example:
The following documents were inserted into the records collections.
db.records.find()
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbf9"), "a" : 5, "b" : 5, "c" : null }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfa"), "a" : 3, "b" : null, "c" : 8 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfb"), "a" : null, "b" : 3, "c" : 9 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfc"), "a" : 1, "b" : 2, "c" : 3 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfd"), "a" : 2, "c" : 5 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbfe"), "a" : 3, "b" : 2 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bbff"), "a" : 4 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bc00"), "b" : 2, "c" : 4 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bc01"), "b" : 2 }
{ "_id" : ObjectId("61b321f9a6f13d3cfc01bc02"), "c" : 6 }
We will apply the below query to return specific fields that do not exist.
db.records.find( { a: { $exists: false } } )
Here, we have used the $exists operator where false means the results consist of those documents that do not contain the field a.

Read: Distinct query in MongoDB
MongoDB return specific fields equal
In this topic, you will learn to return the specific fields or document that’s equal. For this, MongoDB provides the $eq operator that matches documents where the value of a field equals the specified value.
Syntax:
{ <field>: { $eq: <value> } }
Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the details collection.
db.details.find()
{ "_id" : 1, "item" : "journal", "qty" : 15, "tags" : [ "appliances", "school", "clothing" ] }
{ "_id" : 2, "item" : "notebook", "qty" : 20, "tags" : [ "school" ] }
{ "_id" : 3, "item" : "paper", "qty" : 25, "tags" : [ "appliances", "school" ] }
{ "_id" : 4, "item" : "planner", "qty" : 30, "tags" : [ "school", "appliances" ] }
{ "_id" : 5, "item" : "postcard", "qty" : 20, "tags" : [ [ "appliances", "school" ], "clothing" ] }
Now, we will apply the below query to return the specific fields or documents from the collection.
db.details.find( { qty: { $eq: 20 } } )
Here, this query is used to select all the documents where the value of the qty field equals 20.
Output:

Read: MongoDB find by ID
MongoDB return specific field empty
In this topic, you will learn to return specific empty fields in a MongoDB collection. We will use the $exists and $eq operator to check empty fields in a collection.
Let’s get comprehend this with the help of an example.
Example:
The following documents will insert into the returnEmpty collection.
db.returnEmpty.insertMany([
{ "_id" : 101, "FirstName" : "Chris", "LastName" : "" },
{ "_id" : 102, "FirstName" : "Peter", "LastName" : "David" },
{ "_id" : 103, "FirstName" : "Miller", "LastName" : "" },
{ "_id" : 104, "FirstName" : "Brown", "LastName" : " Robert" }
])
We will use the find() method to display all the documents from a collection.

Now, we will use the below query to check the empty field:-
db.returnEmpty.find({"LastName" : {"$exists" : true, "$eq" : ""}})
Here, we have used the { $exists: true, $eq: ” ” } operator for the LastName field. And, the $exists field is used to check the field exists or not and after that, the $eq operator is used to check the field is empty and return all those fields that are empty.

Read: MongoDB compare two fields
MongoDB return specific field regex
In this topic, you will learn to return the specific fields from the MongoDB collection using regular expressions. The $regex operator provides regular expression capabilities for pattern matching strings in queries.
You can use one of the following syntaxes to use the $regex operator.
{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
You can use regular expression objects (/pattern/) to specify regular expressions.
{ <field>: /pattern/<options> }
The following options are available for use with the regular expression:
Option | Description | Syntax Restrictions |
---|---|---|
i | Case insensitivity to match upper and lower cases. | |
m | This is used for patterns that include anchors (i.e. ^ for the start, $ for the end), match at the beginning or end of each line for strings with multiline values. And, if we don’t use this option, these anchors match at the beginning or end of the string. | |
x | It provides the “Extended” capability to ignore all white space characters in the $regex pattern unless escaped or included in a character class. | It requires the $regex with $options syntax |
s | It allows the dot character (i.e. ‘.’) to match all characters including the newline character. | It requires the $regex with $options syntax |
Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the details collection.
db.details.find()
{ "_id" : 1, "item" : "journal", "qty" : 15, "tags" : [ "appliances", "school", "clothing" ] }
{ "_id" : 2, "item" : "notebook", "qty" : 20, "tags" : [ "school" ] }
{ "_id" : 3, "item" : "paper", "qty" : 25, "tags" : [ "appliances", "school" ] }
{ "_id" : 4, "item" : "planner", "qty" : 30, "tags" : [ "school", "appliances" ] }
{ "_id" : 5, "item" : "postcard", "qty" : 20, "tags" : [ [ "appliances", "school" ], "clothing" ] }
Now, we can perform the following operation using the $regex (regular expression) operator.
Note that, MongoDB starting version in 4.0.7 $not operator can perform logical NOT operation.
- regular expression objects (i.e. /pattern/) using $not operator
db.details.find( { item: { $not: /^p.*/ } } )
Here, we have used the $not operator and defined the pattern /^p.*/ that will return all those documents where the item field name does not start with p.

- $regex operator expression with $not operator
db.details.find( { item: { $not: { $regex: /^p.*/ } } } )
Here, we have defined the $regex regular expression pattern /^p.*/ and used the $not operator that performs as a logical NOT operation. This will return those documents where the item field name does not start with P.

- Perfrom case-insensitive regular expression match
The following query is using the “i” option to perform a case-insensitive match for a document with the item value that starts with “j”.
db.details.find( { item: { $regex: /^j/i } } )

Read: MongoDB remove an element from the array
MongoDB return only a specific field
In this topic, you will learn to return only a specific field from the MongoDB collection. The find( ) method is used to return the document of the collection and if you want to return a specific field then you have to define the condition in the find method.
Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the car collection.
db.car.find()
{"_id" : 1, "name" : "Audi", "color" : "Red", "cno" : "C101", "mfdcountry" : "Germany", "speed" : 75 }
{"_id" : 2, "name" : "Swift", "color" : "Black", "cno" : "C102", "mfdcountry" : "Italy", "speed" : 60 }
{"_id" : 3, "name" : "Tesla", "color" : "Blue", "cno" : "C103", "mfdcountry" : "USA", "speed" : 100 }
{"_id" : 4, "name" : "Polo", "color" : "White", "cno" : "C104", "mfdcountry" : "Japan", "speed" : 65 }
{"_id" : 5, "name" : "Audi", "color" : "Black", "cno" : "G101", "mfdcountry" : "Germany", "speed" : 90 }
{"_id" : 6, "name" : "Volkswagen", "color" : "JetBlue", "cno" : "C105", "mfdcountry" : "Rome", "speed" : 80 }
Now, we will apply the below query to return only a specific field from the MongoDB collection.
db.car.find( { "mfdcountry": "USA" } )
Here, We have used the find( ) method that will return documents where the “mfdcountry” is “USA”.

Read: MongoDB find last inserted document
MongoDB aggregate return specific field
In this topic, you will learn to return specific fields using the MongoDB aggregation pipeline. The following syntax is used for the aggregation pipeline.
Syntax:
db.collection.aggregate( pipeline, options )
Parameter | Type | Description |
---|---|---|
pipeline | array | A sequence of data aggregation operations or stages. |
options | document | Optional, we can pass the additional options that aggregate( ) passes to the aggregate command. It is only available if you specify the pipeline as an array. |
Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the example collection.
db.example.find()
{ "_id" : "101", "name" : "James", "age" : "26", "city" : "USA" }
{ "_id" : "102", "name" : "Noah", "age" : "22" }
{ "_id" : "103", "name" : "Robert", "age" : "28", "city" : "United States" }
{ "_id" : "104", "name" : "Peter", "age" : "23", "city" : "Canada" }
{ "_id" : "105", "name" : "Jack", "age" : "25" }
Now, we will use the $match stage and return specific fields from the collection.
db.example.aggregate([
{ $match: { city: "United States" } }
])
Here, we have used the $match operator that filters the documents to pass only those documents that match the specified condition to the next pipeline stage. So, this query will return where the “city” is the “United States”.
Output:

Read: How to change collection name in MongoDB
MongoDB find and return specific field
In this topic, you will learn to find the specific field and return from the MongoDB collection. We will use the find() method to return the documents of the collection.
Let’s get comprehend this with the help of an example.
Example:
The subsequent documents were inserted into the inventory collection.
db.inventory.find()
{ "_id" : 1, "dept" : "A", "item" : { "sku" : "101", "color" : "red" }, "sizes" : [ "S", "M" ] }
{ "_id" : 2, "dept" : "A", "item" : { "sku" : "102", "color" : "blue" }, "sizes" : [ "M", "L" ] }
{ "_id" : 3, "dept" : "B", "item" : { "sku" : "103", "color" : "blue" }, "sizes" : "S" }
{ "_id" : 4, "dept" : "A", "item" : { "sku" : "104", "color" : "black" }, "sizes" : [ "S" ] }
{ "_id" : 5, "dept" : "B", "item" : { "sku" : "104" }, "sizes" : [ "S", "L" ] }
Now, we will apply the below query to find and return the specific fields or documents from the collection.
db.inventory.find( { "item.color" : "blue" } )
Here, we accessed the nested field from the array “item.color” and return the document where “color” is “blue”.
Output:

Read: How to get id from mongodb after insert
MongoDB return specific fields of documents
In this topic, you will learn to return specific fields of documents from the MongoDB collection. When multiple documents fulfill the condition then all are returned by using the find() method.
Let’s get comprehend this with the help of an example.
Example:
The subsequent documents were inserted into the inventory collection.
db.inventory.find()
{ "_id" : 1, "dept" : "A", "item" : { "sku" : "101", "color" : "red" }, "sizes" : [ "S", "M" ] }
{ "_id" : 2, "dept" : "A", "item" : { "sku" : "102", "color" : "blue" }, "sizes" : [ "M", "L" ] }
{ "_id" : 3, "dept" : "B", "item" : { "sku" : "103", "color" : "blue" }, "sizes" : "S" }
{ "_id" : 4, "dept" : "A", "item" : { "sku" : "104", "color" : "black" }, "sizes" : [ "S" ] }
{ "_id" : 5, "dept" : "B", "item" : { "sku" : "104" }, "sizes" : [ "S", "L" ] }
Now, we will apply the below query to find and return specific fields of documents from the collection.
db.inventory.find( { "dept" : "A" } )
The above query will return all the documents where the dept is “A”.

Read: MongoDB date format
MongoDB return specific fields greater than
In this topic, you will learn to return the specific field or document from the MongoDB collection that is greater than a particular field.
We will use the $gt operator that selects those documents where the value of the field is greater than the specified value.
Syntax:
{field: {$gt: value} }
Let’s get comprehend this with the help of an example.
Example:
The subsequent documents were inserted into the inventory collection.
db.inventory.find()
{ "_id" : 1, "item" : "abc1", "description" : "product 1", "qty" : 300 }
{ "_id" : 2, "item" : "abc2", "description" : "product 2", "qty" : 200 }
{ "_id" : 3, "item" : "xyz1", "description" : "product 3", "qty" : 250 }
{ "_id" : 4, "item" : "VWZ1", "description" : "product 4", "qty" : 300 }
{ "_id" : 5, "item" : "VWZ2", "description" : "product 5", "qty" : 200 }
{ "_id" : 6, "item" : "xyz1", "description" : "product 6", "qty" : 250 }
Now, we will apply the below query to return specific fields which is greater than a particular field from the documents of the MongoDB collection.
db.inventory.find( { qty: { $gt: 200 } } )
The above query will return all the documents where the “qty” field value is greater than “200”.

Read: MongoDB check if the document exists
MongoDB lookup return specific fields
MongoDb provides the $lookup aggregation function for joining the two collections. In this topic, you will learn to return the specific fields or documents from the MongoDB collection using the $lookup aggregation function.
This function will help us to join the documents of the two collections that reside in the same database. Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the address and userInfo collections respectively.
db.address.find()
{
"_id" : ObjectId("61b993bc181ae586b7fb6f9d"),
"name" : "Bob",
"blk_no" : 22,
"street" : "dewey street",
"city" : "United States of America"
}
{
"_id" : ObjectId("61b993bc181ae586b7fb6f9e"),
"name" : "Jack",
"blk_no" : 25,
"street" : "gordon street",
"city" : "New Zealand"
}
db.userInfo.find()
"_id" : ObjectId("61b993d2181ae586b7fb6f9f"),
"contact_name" : "Bob",
"age" : 27,
"sex" : "male",
"citizenship" : "Filipino"
}
{
"_id" : ObjectId("61b993d2181ae586b7fb6fa0"),
"contact_name" : "Jack",
"age" : 22,
"sex" : "male",
"citizenship" : "Filipino"
}
Note that, the name field in the address collection has the same values as the contact_name in the userInfo collection.
Now, we apply the $lookup function and find the result as an equality match.
db.userInfo.aggregate([
{ $lookup:
{
from: "address",
localField: "contact_name",
foreignField: "name",
as: "address"
}
}
]).pretty()
Here, we have used the $lookup aggregation to join two collections where we match the equality in the name and contact_name field.
Output:

Read: How to delete documents in MongoDB
MongoDB return a specific field not null
In this topic, you will learn to return a specific field not null. You can use the $ne operator to check for not null. Let’s get comprehend this with the help of an example.
Example:
The following documents will insert into the notNull collection.
db.notNull.insertMany([
{"LoginUserName":"Jack","LoginPassword":"Jack_12"},
{"LoginUserName":"Jack","LoginPassword":null},
{"LoginUserName":"Jack","LoginPassword":""}
])
After inserting the documents into the notNull collection will return all the documents using the find() method.

Now, we will apply the below return a specific field not null
db.notNull.find(
{
"LoginUserName":"Jack",
"LoginPassword" : { '$ne': null }
}
)
Here, we have used the $ne operator that check for not null field value and return the specific field where not null.

Read: MongoDB nested query
MongoDB return specific fields not equal
In this topic, you will learn to return the specific fields that are not equal. MongoDB provides the $ne operator that is used to select the documents where the value of the field is not equal to the specified value. This contains the documents that do not contain the field.
We can use this operator in methods like find(), update(), etc. as per the requirement.
Syntax:
{ field: { $ne: value }}
Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the person collection.
db.person.find()
{ "_id" : ObjectId("61a6f53f4de69d7af5abb103"), "name" : "James", "age" : 25, "gender" : "male", "city" : "United States" }
{ "_id" : ObjectId("61a6f53f4de69d7af5abb104"), "name" : "John", "age" : 28, "gender" : "male", "city" : "Canada" }
{ "_id" : ObjectId("61a6f53f4de69d7af5abb105"), "name" : "Mary", "age" : 33, "gender" : "female", "city" : "Canada" }
{ "_id" : ObjectId("61a6f53f4de69d7af5abb106"), "name" : "David", "age" : 22, "gender" : "male", "city" : "Australia" }
{ "_id" : ObjectId("61a6f53f4de69d7af5abb107"), "name" : "Jessica", "age" : 27, "gender" : "female", "city" : "USA" }
{ "_id" : ObjectId("61a6f53f4de69d7af5abb108"), "name" : "Daniel", "age" : 27, "gender" : "male", "city" : "United Kingdom" }
Now, we will apply the below query to return the specific fields that are not equal.
db.person.find( { age: { $ne: 27 } } )
Here, this query will select all the documents in the person collection where the age field value does not equal 27, including those documents that do not contain the age field.

Read: Select the first element in an array in MongoDB
MongoDB return a specific field max value
In this topic, we will learn to return a specific field of MongoDB collection where the max value of a column. We get the max value of a column using the sort() method along with the limit(1).
The following syntax will use to get the highest value of a column in MongoDB.
Syntax:
db.CollectionName.find().sort({"FieldName":-1}).limit(1)
Let’s get comprehend this with the help of an example.
Example:
The following documents were inserted into the relation collection.
db.relation.find()
{ "_id" : ObjectId("6165078e016e7b648abb7fa6"), "subject" : "Joe", "content" : "best friend", "likes" : 60, "year" : 2015, "language" : "english" }
{ "_id" : ObjectId("6165078e016e7b648abb7fa7"), "subject" : "Dogs", "content" : "Cats", "likes" : 30, "year" : 2015, "language" : "english" }
{ "_id" : ObjectId("6165078e016e7b648abb7fa8"), "subject" : "Cats", "content" : "Rats", "likes" : 55, "year" : 2014, "language" : "english" }
{ "_id" : ObjectId("6165078e016e7b648abb7fa9"), "subject" : "Rats", "content" : "Joe", "likes" : 75, "year" : 2014, "language" : "english" }
Now, we will apply the below query to return a specific field of MongoDB collection where the max value of a column.
db.relation.find().sort( { "likes": -1 } ).limit(1)
Here, the above query is used to get the highest value of a column in MongoDB.
Output:

MongoDB return specific fields using python
In this topic, you will learn to return the specific field of a MongoDB collection using python. Let’s get comprehend this with the help of an example.
Example:
The following block of code is used to return the specific field of the MongoDB collection.
In the python code, first, we import the pymongo library to access the MongoDB database.
- After that, we have created a MongoClient object and connect with the host.
- Next, Access the database and collection “test” and “marks” respectively.
- The following documents were inside the “marks” collection.
{ "_id" : 1, "subject" : "History", "score" : 88 }
{ "_id" : 2, "subject" : "Science", "score" : 92 }
{ "_id" : 3, "subject" : "Math", "score" : 97 }
{ "_id" : 4, "subject" : "English", "score" : 88 }
{ "_id" : 5, "subject" : "Hindi", "score" : 69 }
{ "_id" : 6, "subject" : "Social Science", "score" : 80 }
{ "_id" : 7, "subject" : "Computer", "score" : 73 }
- After that, we have used the $lt operator to return the documents where the score are less than 80.
- Next, we will display all the specific fields where score are less than 80.
import pymongo
# creating a MongoClient object and connect with the host
myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017")
# accessing the database
mydb = myclient['test']
# accessing the collection of the database
mycol = mydb["marks"]
field = mycol.find({ "score": { "$lt": 80 } })
# return specific fields as per condition
for returnField in field:
print(returnField)
Output:

You may also like to read the following tutorials on MongoDB.
- MongoDB aggregate $count with examples
- MongoDB find string contains
- MongoDB count with examples
- MongoDB get collection size
I hope you have learned about MongoDB return specific fields with a few examples and cover the below topics:
- MongoDB query return specific fields
- MongoDB return specific fields with any value
- MongoDB return specific fields that exists
- MongoDB return specific field not exists
- MongoDB return specific fields equal
- MongoDB return specific field empty
- MongoDB return specific field regex
- MongoDB return only a specific field
- MongoDB aggregate return specific field
- MongoDB find and return specific field
- MongoDB return specific fields of documents
- MongoDB return specific fields greater than
- MongoDB lookup return specific fields
- MongoDB return a specific field not null
- MongoDB return specific fields not equal
- MongoDB return a specific field max value
- MongoDB return specific fields using python
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.