
- 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
Specify return format in MongoDB to return the values as an array?
Use aggregation for this and add the values to an array using the $group and $addToSet operator
Let us first create a collection with documents −
> dbspecifyReturnFormatDemoinsertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefd364ef71edecf6a1f6c0") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefd369ef71edecf6a1f6c1") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"SQL Server"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefd36fef71edecf6a1f6c2") }
Following is the query to display all documents from a collection with the help of find() method −
> dbspecifyReturnFormatDemofind();
Output
{ "_id" : ObjectId("5cefd364ef71edecf6a1f6c0"), "Subject" : "MongoDB" } { "_id" : ObjectId("5cefd369ef71edecf6a1f6c1"), "Subject" : "MySQL" } { "_id" : ObjectId("5cefd36fef71edecf6a1f6c2"), "Subject" : "SQL Server" }
Following is the query to specify return format −
> dbspecifyReturnFormatDemoaggregate([ { "$group": { "_id": 0, "Subject": { "$addToSet": "$Subject" } } }, { "$project": { "_id": 0, "Subject": 1 } } ]);
Output
{ "Subject" : [ "SQL Server", "MySQL", "MongoDB" ] }
- Related Articles
- Specify a return format for data in MongoDB
- MongoDB query to return specific fields from an array?
- Return the gradient of an N-dimensional array and specify edge order in Python
- Return indexes of greatest values in an array in JavaScript
- Return the data of a masked array as an ndarray
- Get the first element in an array and return using MongoDB Aggregate?
- How to return only unique values (no duplicates) in MongoDB?
- MongoDB query to return only specific fields (phone numbers) in the form of an array?
- Select two fields and return a sorted array with their distinct values in MongoDB?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- Haskell Program to return an array from the function
- Return an array of ones with the same shape and type as a given array in Numpy
- Return an array of zeros with the same shape and type as a given array in Numpy
- Return the fractional and integral parts of array values in Numpy
- Return the Lower triangle of an array in Numpy

Advertisements