
- 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 function to return a specific data/value?
To return a specific data, use findOne() in MongoDB. The findOne() method returns one document that satisfies the specified query criteria on the collection Let us create a collection with documents −
> db.demo473.insertOne( ... { ... "_id" : new ObjectId(), ... "Name" : "Chris", ... "details" : { ... "X-Coordinate" :10, ... "Y-Coordinate" :15 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e805a07b0f3fa88e227907d") } > db.demo473.insertOne( ... { ... "_id" : new ObjectId(), ... "Name" : "Bob", ... "details" : { ... "X-Coordinate" :11, ... "Y-Coordinate" :12 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e805a07b0f3fa88e227907e") }
Display all documents from a collection with the help of find() method −
> db.demo473.find();
This will produce the following output −
{ "_id" : ObjectId("5e805a07b0f3fa88e227907d"), "Name" : "Chris", "details" : { "X-Coordinate" : 10, "Y-Coordinate" : 15 } } { "_id" : ObjectId("5e805a07b0f3fa88e227907e"), "Name" : "Bob", "details" : { "X-Coordinate" : 11, "Y-Coordinate" : 12 } }
Following is the query to return a specific data with MongoDB findOne() function −
> db.demo473.findOne({ 'details.X-Coordinate':11 })
This will produce the following output −
{ "_id" : ObjectId("5e805a07b0f3fa88e227907e"), "Name" : "Bob", "details" : { "X-Coordinate" : 11, "Y-Coordinate" : 12 } }
- Related Articles
- Return a specific field in MongoDB?
- Return specific MongoDB embedded document
- MongoDB query to return specific fields from an array?
- Find value above a specific value in MongoDB documents?
- MongoDB query to pull a specific value from a document
- How to return a value from a JavaScript function?
- MongoDB query to increment a specific value using custom variable
- Query an array in MongoDB to fetch a specific value
- MongoDB query (aggregation framework) to match a specific field value
- Specify a return format for data in MongoDB
- How to return only value of a field in MongoDB?
- Update only a specific value in a MongoDB document
- Is it possible to return a list of specific values from a MongoDB query?
- MongoDB inverse of query to return all items except specific documents?
- MongoDB query to determine if a specific value does not exist?

Advertisements