
- 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
Is it possible to return a list of specific values from a query in MongoDB?
To return a list of specific values, use map(). Let us create a collection with documents −
> db.demo195.insertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3af3f203d395bdc21346d4") } > db.demo195.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3af3f703d395bdc21346d5") } > db.demo195.insertOne({"Subject":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3af3fa03d395bdc21346d6") }
Display all documents from a collection with the help of find() method −
> db.demo195.find();
This will produce the following output −
{ "_id" : ObjectId("5e3af3f203d395bdc21346d4"), "Subject" : "MySQL" } { "_id" : ObjectId("5e3af3f703d395bdc21346d5"), "Subject" : "MongoDB" } { "_id" : ObjectId("5e3af3fa03d395bdc21346d6"), "Subject" : "Java" }
Following is the query to return a list of specific values from a query −
> db.demo195.find().map(function(doc){ return doc.Subject });
This will produce the following output −
[ "MySQL", "MongoDB", "Java" ]
- Related Articles
- Is it possible to return a list of specific values from a MongoDB query?
- Is it possible to cast in a MongoDB Query?
- MongoDB query to return specific fields from an array?
- Is it possible to make a case-insensitive query in MongoDB?
- MongoDB query to add up the values of a specific field in documents
- MongoDB query to update a specific document from a collection
- MongoDB query to pull a specific value from a document
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Filter specific values from a MongoDB document
- How to get values greater than a specific value from an embedded list in MongoDB?
- MongoDB query to find a specific city record from a collection
- MongoDB query to 'sort' and display a specific number of values
- MongoDB inverse of query to return all items except specific documents?
- Return a specific field in MongoDB?
- MongoDB query to remove a specific document

Advertisements