
- 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
Working with MongoDB $concatArrays in $project on existing multi-array field
The $concatArrays is used to concatenate arrays to return the concatenated array.
Let us create a collection with documents −
> db.demo338.insertOne({"Name":"Chris","Marks1":[ [56,67,45],[67,89,90,91]]}); { "acknowledged" : true, "insertedId" : ObjectId("5e5299baf8647eb59e56209f") }
Display all documents from a collection with the help of find() method −
> db.demo338.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e5299baf8647eb59e56209f"), "Name" : "Chris", "Marks1" : [ [ 56, 67, 45 ], [ 67, 89, 90, 91 ] ] }
Following is the query to work on the existing multi-array field and concatenate arrays −
> db.demo338.aggregate([ ... { "$project": { ... "Marks": { ... "$reduce": { ... "input": "$Marks1", ... "initialValue": [], ... "in": { "$concatArrays": ["$$this", "$$value"] } ... } ... } ... }} ... ])
This will produce the following output &minus'
{ "_id" : ObjectId("5e5299baf8647eb59e56209f"), "Marks" : [ 67, 89, 90, 91, 56, 67, 45 ] }
- Related Articles
- How to project specific elements in an array field with MongoDB?
- Project specific array field in a MongoDB collection?
- Multi-key Indexing on an entire array with MongoDB?
- Project field in MongoDB
- Working with MongoDB $sort for sub array document
- Pushing values into array with multi field set to TRUE?
- Replace an array field value with MongoDB?
- Reverse array field in MongoDB?
- MongoDB query to update array with another field?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- How do I add a field to existing record in MongoDB?
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- MongoDB slice array in populated field?
- Working with MongoDB find()
- Query a nested field within an array with MongoDB

Advertisements