
- 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
Retrieve only a single document specifying a criteria in MongoDB?
Use findOne() in MongoDB for this. The findOne() returns one document that satisfies the specified query criteria on the collection.
Let us create a collection with documents −
> db.demo596.insertOne({_id:1,"FirstName":"John","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo596.insertOne({_id:2,"FirstName":"John","LastName":"Doe"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo596.insertOne({_id:3,"FirstName":"Chris","LastName":"Brown"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo596.insertOne({_id:4,"FirstName":"David","LastName":"Miller"}); { "acknowledged" : true, "insertedId" : 4 }
Display all documents from a collection with the help of find() method −
> db.demo596.find();
This will produce the following output −
{ "_id" : 1, "FirstName" : "John", "LastName" : "Smith" } { "_id" : 2, "FirstName" : "John", "LastName" : "Doe" } { "_id" : 3, "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : 4, "FirstName" : "David", "LastName" : "Miller" }
Here is the query to retrieve only a single document −
> db.demo596.findOne({"FirstName":"John"});
This will produce the following output −
{ "_id" : 1, "FirstName" : "John", "LastName" : "Smith" }
- Related Articles
- How to find only a single document satisfying the 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
- Update only a specific value in a MongoDB document
- Decrement only a single value in MongoDB?
- Update a single list item of a MongoDB document?
- Selecting only a single field from MongoDB?
- Updating a MongoDB document and adding new keys only in the first document?
- Remove only one document in MongoDB?
- How to return only a single property “_id” in MongoDB?
- How to remove all documents from a collection except a single document in MongoDB?

Advertisements