How to retrieve documents from a collection in MongoDB?


To retrieve documents from a collection in MongoDB, you need to use find() method. The syntax is as follows:

db.yourCollectionName.find();

The above syntax will return all the documents from a collection in MongoDB. To understand the above syntax, let us create a collection with documents. The query to create documents are as follows:

> db.retrieveAllStudents.insertOne({"StudentId":"STUD101","StudentName":"David","StudentAge":24});
{
   "acknowledged" : true, "insertedId" : ObjectId("5c6bf5cf68174aae23f5ef4e")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD102","StudentName":"Carol","StudentAge":22});
{
   "acknowledged" : true, "insertedId" : ObjectId("5c6bf5e968174aae23f5ef4f")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD103","StudentName":"Maxwell","StudentAge":25});
{
   "acknowledged" : true, "insertedId" : ObjectId("5c6bf5f768174aae23f5ef50")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD104","StudentName":"Bob","StudentAge":23});
{
   "acknowledged" : true, "insertedId" : ObjectId("5c6bf60868174aae23f5ef51")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD105","StudentName":"Sam","StudentAge":27});
{
   "acknowledged" : true, "insertedId" : ObjectId("5c6bf61b68174aae23f5ef52")
}

Now you can use the above syntax in order to retrieve all the documents from a collection with the help of find() method. The query is as follows:

> db.retrieveAllStudents.find();

The following is the output:

{ "_id" : ObjectId("5c6bf5cf68174aae23f5ef4e"), "StudentId" : "STUD-101", "StudentName" :
   "David", "StudentAge" : 24 }
{ "_id" : ObjectId("5c6bf5e968174aae23f5ef4f"), "StudentId" : "STUD-102", "StudentName" :
   "Carol", "StudentAge" : 22 }
{ "_id" : ObjectId("5c6bf5f768174aae23f5ef50"), "StudentId" : "STUD-103", "StudentName" :
   "Maxwell", "StudentAge" : 25 }
{ "_id" : ObjectId("5c6bf60868174aae23f5ef51"), "StudentId" : "STUD-104", "StudentName" :
   "Bob", "StudentAge" : 23 }
{ "_id" : ObjectId("5c6bf61b68174aae23f5ef52"), "StudentId" : "STUD-105", "StudentName" :
   "Sam", "StudentAge" : 27 }

For a proper formatted output, use pretty() with find(). The query is as follows:

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

The following is the output:

{
   "_id" : ObjectId("5c6bf5cf68174aae23f5ef4e"),
   "StudentId" : "STUD-101",
   "StudentName" : "David",
   "StudentAge" : 24
}
{
   "_id" : ObjectId("5c6bf5e968174aae23f5ef4f"),
   "StudentId" : "STUD-102",
   "StudentName" : "Carol",
   "StudentAge" : 22
}
{
   "_id" : ObjectId("5c6bf5f768174aae23f5ef50"),
   "StudentId" : "STUD-103",
   "StudentName" : "Maxwell",
   "StudentAge" : 25
}
{
   "_id" : ObjectId("5c6bf60868174aae23f5ef51"),
   "StudentId" : "STUD-104",
   "StudentName" : "Bob",
   "StudentAge" : 23
}
{
   "_id" : ObjectId("5c6bf61b68174aae23f5ef52"),
   "StudentId" : "STUD-105",
   "StudentName" : "Sam",
   "StudentAge" : 27
}

If you want to retrieve a single document on the basis of some condition, then you can use the following query. Here, we are retrieving the document with StudentName as “Maxwell”:

> db.retriveAllStudents.find({"StudentName":"Maxwell"}).pretty();

The following is the output:

{
   "_id" : ObjectId("5c6bf5f768174aae23f5ef50"),
   "StudentId" : "STUD-103",
   "StudentName" : "Maxwell",
   "StudentAge" : 25
}

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements