
- 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
MongoDB findById returning a list of documents instead of a single result? How to get only a single document?
To get only a single result, use findOne() and fetch on the basis of id. Let us create a collection with documents −
> db.demo340.insertOne({_id:1,"Name":"Chris",Age:21}); { "acknowledged" : true, "insertedId" : 1 } > db.demo340.insertOne({_id:2,"Name":"David",Age:23}); { "acknowledged" : true, "insertedId" : 2 } > db.demo340.insertOne({_id:3,"Name":"Bob",Age:20}); { "acknowledged" : true, "insertedId" : 3 } > db.demo340.insertOne({_id:4,"Name":"Sam",Age:19}); { "acknowledged" : true, "insertedId" : 4 }
Display all documents from a collection with the help of find() method −
> db.demo340.find();
This will produce the following output −
{ "_id" : 1, "Name" : "Chris", "Age" : 21 } { "_id" : 2, "Name" : "David", "Age" : 23 } { "_id" : 3, "Name" : "Bob", "Age" : 20 } { "_id" : 4, "Name" : "Sam", "Age" : 19 }
Here is the query to findby id −
> db.demo340.findOne({_id:1});
This will produce the following output −
{ "_id" : 1, "Name" : "Chris", "Age" : 21 }
- Related Articles
- Update a single list item of a MongoDB document?
- Update only a single document in MongoDB
- Remove only a single document in MongoDB
- Increment only a single value in MongoDB document?
- How to remove all documents from a collection except a single document in MongoDB?
- Retrieve only a single document specifying a criteria in MongoDB?
- How to find only a single document satisfying the criteria in MongoDB?
- Update only a single MongoDB document without deleting any date
- How to store query result (a single document) into a variable?
- MongoDB findOneAndUpdate() to update a single document
- Display only a single field from all the documents in a MongoDB collection
- How can I sort documents in MongoDB 4 and display only a single field?
- MongoDB aggregate to convert multiple documents into single document with an array?
- How to fetch only a single result from a table in Java-MySQL?
- How to return only a single property “_id” in MongoDB?

Advertisements