
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 }
- Related Articles
- How to retrieve all the documents from a MongoDB collection using Java?
- How to delete documents from a collection in MongoDB?
- Retrieve data from a MongoDB collection?
- How to retrieve all nested fields from MongoDB collection?
- How to delete all the documents from a collection in MongoDB?
- How to remove all documents from a collection except a single document in MongoDB?
- Fetching all documents from MongoDB Collection in a beautified form
- Can I retrieve multiple documents from MongoDB by id?
- Find a value in lowercase from a MongoDB collection with documents
- Retrieving specific documents from collection by _id in MongoDB
- How to count the number of documents in a MongoDB collection?
- How to Update multiple documents in a MongoDB collection using Java?
- How to return documents of a collection without objectId in MongoDB?
- Count number of documents from MongoDB collection inside Array?
- MongoDB query to retrieve records from a collection named with letters and numbers
