The collection.find() always returns all fields with MongoDB?


You can return a specific field from collection.find() by using the following syntax.

Case 1 − The syntax is as follows −

db.yourCollectionName.find({},{"yourFieldName":1}).pretty();

The above field name is set to 1 means it will return only that field. If you set to 0 it will return all fields except the field which is set to 0.

Case 2 − The syntax is as follows −

db.yourCollectionName.find({},{"yourFieldName":0}).pretty();

To understand the above syntaxes, let us create a collection with document. The query to create a collection with document is as follows −

> db.returnFieldInFindDemo.insertOne({"StudentName":"John","StudentAge":23,"TechnicalSubject":["MongoDB","MySQL"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ebfe72f684a30fbdfd566")
}
> db.returnFieldInFindDemo.insertOne({"StudentName":"Mike","StudentAge":24,"TechnicalSubject":["Java","Python"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ebffd2f684a30fbdfd567")
}
> db.returnFieldInFindDemo.insertOne({"StudentName":"Sam","StudentAge":22,"TechnicalSubject":["C","C++"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ec00f2f684a30fbdfd568")
}
> db.returnFieldInFindDemo.insertOne({"StudentName":"Carol","StudentAge":20,"TechnicalSubject":["DataStructure","Algorithm"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ec0292f684a30fbdfd569")
}

Display all documents from a collection with the help of find() method. The query is as follows −

> db.returnFieldInFindDemo.find().pretty();

The following is the output −

{
   "_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"
   ]
}

Case 1 − Here is the query by which you will return specific field.

The query is as follows −

> db.returnFieldInFindDemo.find().pretty();

The output:

{
   "_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"
   ]
}

Look at the above sample output, I have initialized the field “TechnicalSubject” to 1. This means it will return only the field “TechnicalSubject” from all documents.

Case 2 − In the second case, if you set the field “TechnicalSubject” to 0 that means you will get all fields except the “TechnicalSubject”.

The query is as follows −

> db.retunFieldInFindDemo.find({},{"TechnicalSubject":0}).pretty();

The following is the output:

{
   "_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 − As you know, if you use only to find() then it returns all fields.

The query is as follows −

> db.retunFieldInFindDemo.find();

The following is the output:

{ "_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" ] }

Updated on: 30-Jul-2019

462 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements