How to select a single field from all documents in a MongoDB collection? In this article, we will walk through how to use some methods in MongoDB to select a single field from all documents in a collection. Significantly, we have methods that will help us to accomplish this task.
First of all, we are required the collection that contains some documents. Assume the collection student
with the following documents.
# Create Collection
db.student.insertMany([
{
_id: 1,
name: "Emily",
age: 22 ,
subject: "pre-medical"
},
{
_id: 2,
name: "Nina",
age: 23 ,
subject: "computer"
},
{
_id: 3,
name: "stefan",
age: 24 ,
subject: "pre-engineering"
},
{
_id: 4,
name: "Alice",
age: 25 ,
subject: "computer"
}
])
1. Using the find() Method to Select the Single Field
You can use the MongoDB find()
method along with the projection parameter to select the single field from the document. This approach is very common to select a single field from all documents in a MongoDB collection.
Here is the query for this, we called the find()
method first which receives all the documents, then, the expression { name: 1, _id: 0 }
specifies the projection to only include the name
field and exclude the default _id
field. The value 1
indicates that the field should be included in the output, while 0
indicates that it should be excluded.
# Usage of find() method to select single field
db.student.find({}, { name: 1, _id: 0 })
As we can see, the single field name
is showed up in the output.
More details about the find() method can be fiund here.
2. Using the Condition to Select the Single Field
However, we can select a single field from all documents which meet the specified condition, to get this use the find()
method with the first parameter as a condition and the second parameter as a projection.
In the following example, the find()
method retrieved the documents where the subject
field is computer
and includes only the field age
in the result. As the age
field is given a value 1
which indicates selecting the field and 0
indicates deselecting the field.
# Usage of condition
db.student.find({"subject":"computer"},{"age" : 1, _id:0}).pretty();
Thus, the output showed two documents with the single field age
that satisfy the criteria.
3. Using the $project operator to select the single field:
We can also select a single field from all documents in a collection using the aggregate()
method with the $project
operator in MongoDB. The aggregate()
method performs aggregation operations on the documents in the collection.
Then, the $project
operator is used to include only the field we want to select in the resulting documents. Consider the following query, the aggregate()
method called the $project
operator that includes only the subject
field in the resulting documents. Notice, the _id
field is excluded using _id: 0
in the $project
operator to make the output readable.
# Usage of $project operator
db.student.aggregate([{ $project: { subject: 1, _id: 0 } }])
The single field subject
of all the documents has been yielded in the output.
4. Using the distinct() Method to Select Single Field
Likewise, we have a distinct()
method to select a single field from all the documents in a collection. The distinct method can only be used on a single field at a time and returned an array of different values. The field for which we want to obtain the unique values must be specified in the distinct()
method.
In the case here, we have used the age
field in the distinct()
method which will return the array of distinct values for the age
field.
# Usage of distinct() method
db.student.distinct("age")
The array of distinct field age
values have been retrieved in the output.
5. Using the find() method with forEach() method
Lastly, we have the forEach()
method that is called in conjunction with the find()
method to select a single field. The forEach()
method is used to loop through each document in the cursor and perform a function on each document.
Here is the query for this, we have retrieved only the name
field from the collection. In this case, we are using the print()
function to print out the value of the name
field for each document.
# Usage of forEach() method
db.student.find().forEach(function(doc) {
print(doc.name);
});
This example yields the below output.
6. Conclusion:
All in all, the find()
method, the distinct()
method, the forEach()
method, and the aggregation framework are all powerful tools that allow us to perform the task of retrieving a single field from all the documents in MongoDB. By using these methods, we easily make the result clearer and more readable.