
- 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 find only a single document satisfying the criteria in MongoDB?
To find only a single document, use findOne() in MongoDB. Let us create a collection with documents −
> db.demo116.insertOne({"EmployeeId":101,"EmployeeName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2eff98d8f64a552dae635b") } > db.demo116.insertOne({"EmployeeId":102,"EmployeeName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2eff9fd8f64a552dae635c") } > db.demo116.insertOne({"EmployeeId":103,"EmployeeName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2effa5d8f64a552dae635d") } > db.demo116.insertOne({"EmployeeId":102,"EmployeeName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2effb7d8f64a552dae635e") }
Display all documents from a collection with the help of find() method −
> db.demo116.find();
This will produce the following output −
{ "_id" : ObjectId("5e2eff98d8f64a552dae635b"), "EmployeeId" : 101, "EmployeeName" : "John" } { "_id" : ObjectId("5e2eff9fd8f64a552dae635c"), "EmployeeId" : 102, "EmployeeName" : "Bob" } { "_id" : ObjectId("5e2effa5d8f64a552dae635d"), "EmployeeId" : 103, "EmployeeName" : "David" } { "_id" : ObjectId("5e2effb7d8f64a552dae635e"), "EmployeeId" : 102, "EmployeeName" : "Mike" }
Following is the query to find only a single document in MongoDB −
> db.demo116.findOne({"EmployeeId":102});
This will produce the following output −
{ "_id" : ObjectId("5e2eff9fd8f64a552dae635c"), "EmployeeId" : 102, "EmployeeName" : "Bob" }
- Related Articles
- Retrieve only a single document specifying a criteria in MongoDB?
- Remove only a single document in MongoDB
- Update only a single document in MongoDB
- Increment only a single value in MongoDB document?
- Update only a single MongoDB document without deleting any date
- MongoDB findById returning a list of documents instead of a single result? How to get only a single document?
- MongoDB findOneAndUpdate() to update a single document
- How to return only a single property “_id” in MongoDB?
- Remove only one document in MongoDB?
- Updating a MongoDB document and adding new keys only in the first document?
- MongoDB query to return only embedded document?
- Decrement only a single value in MongoDB?
- Update only a specific value in a MongoDB document
- How to remove all documents from a collection except a single document in MongoDB?
- How to find a certain element in the MongoDB embedded document?

Advertisements