
- 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
How to get only values in arrays with MongoDB aggregate?
Let us create a collection with documents −
> db.demo411.insertOne( ... { ... "Information" : [ ... { ... "Name1" : "Chris", ... "Name2" : "David" ... }, ... { ... "Name1" : "John", ... "Name2" : "John" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e70f19715dc524f70227682") }
Display all documents from a collection with the help of find() method −
> db.demo411.find();
This will produce the following output −
{ "_id" : ObjectId("5e70f19715dc524f70227682"), "Information" : [ { "Name1" : "Chris", "Name2" : "David" }, { "Name1" : "John", "Name2" : "John" } ] }
Following is the query to get only values in arrays −
> db.demo411.aggregate( ... [ ... {$project : { ... _id : 0, ... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}} ... } ... } ... ] ... )
This will produce the following output −
{ "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] }
- Related Articles
- Aggregate multiple arrays into one huge array with MongoDB?
- MongoDB query to get only distinct values
- Get substring in MongoDB aggregate
- Get the average of marks in MongoDB with aggregate?
- How to calculate sum in MongoDB with aggregate()?
- How to aggregate sum in MongoDB to get the total count?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- To Aggregate Totals in One Group with MongoDB
- How to aggregate array documents in MongoDB?
- How to update after aggregate in MongoDB?
- Use MongoDB Aggregate and select only top record (descending)
- How to get the intersection of two arrays in MongoDB?
- How to get values of cursor in MongoDB?
- How to return only unique values (no duplicates) in MongoDB?
- MongoDB aggregate $slice to get the length of the array

Advertisements