- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it possible to return a list of specific values from a MongoDB query?
Yes, using map(). Let us first create a collection with documents −
> dblistOfSpecificValuesDemoinsertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc8fef71edecf6a1f6bb") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc94ef71edecf6a1f6bc") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc98ef71edecf6a1f6bd") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc9cef71edecf6a1f6be") }
Following is the query to display all documents from a collection with the help of find() method −
> dblistOfSpecificValuesDemofind();
Output
{ "_id" : ObjectId("5cefcc8fef71edecf6a1f6bb"), "StudentName" : "John" } { "_id" : ObjectId("5cefcc94ef71edecf6a1f6bc"), "StudentName" : "Chris" } { "_id" : ObjectId("5cefcc98ef71edecf6a1f6bd"), "StudentName" : "Robert" } { "_id" : ObjectId("5cefcc9cef71edecf6a1f6be"), "StudentName" : "David" }
Following is the query to return a list of specific values from a query −
> dblistOfSpecificValuesDemofind()map(function(myDocument){ return myDocumentStudentName });
Output
[ "John", "Chris", "Robert", "David" ]
- Related Articles
- Is it possible to return a list of specific values from a query in MongoDB?
- 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 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
- MongoDB query to 'sort' and display a specific number of values
- MongoDB query to add up the values of a specific field in documents
- MongoDB query to find a specific city record from a collection
- MongoDB inverse of query to return all items except specific documents?
- How to get values greater than a specific value from an embedded list in MongoDB?
- MongoDB query to remove a specific document
- MongoDB query to match documents with array values greater than a specific value

Advertisements