
- 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 can I concatenate an array of integer in MongoDB aggregation method?
To concatenate, use $concat in MongoDB aggregate(). Let us first create a collection with documents −
> db.demo377.insertOne({"ListOfIds":[1001,1002,1003,1004,1005,1006,1007]}); { "acknowledged" : true, "insertedId" : ObjectId("5e5a73462ae06a1609a00b0e") }
Display all documents from a collection with the help of find() method −
> db.demo377.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e5a73462ae06a1609a00b0e"), "ListOfIds" : [ 1001, 1002, 1003, 1004, 1005, 1006, 1007 ] }
Following is the query to concatenate an array of integer in aggregation −
> db.demo377.aggregate([ ... { "$project": { ... "ListOfIds": { ... "$let": { ... "vars": { ... "key": { ... "$reduce": { ... "input": "$ListOfIds", ... "initialValue": "", ... "in": { "$concat": ["$$value", "++", { "$toLower": "$$this" }] } ... } ... } ... }, ... "in": { "$substrCP": ["$$key", 2, { "$strLenCP": "$$key" }] } ... } ... } ... }} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e5a73462ae06a1609a00b0e"), "ListOfIds" : "1001++1002++1003++1004++1005++1006++1007" }
- Related Articles
- How to add together a subset of elements of an array in MongoDB aggregation?
- How to search documents through an integer array in MongoDB?
- MongoDB Aggregation to slice array inside array
- MongoDB aggregation with equality inside array?
- MongoDB aggregation to sum individual properties on an object in an array across documents
- MongoDB query to get average in aggregation of array element?
- How can I concatenate two arrays in java
- MongoDB aggregation framework to sort by length of array?
- How can I find documents in MongoDB based on the number of matched objects within an array?
- How can I delete an item from an Object in MongoDB?
- Perform MongoDB array concatenation to concatenate records
- MongoDB aggregation group and remove duplicate array values?
- How can I use $elemMatch on first level array in MongoDB?
- How to group nested fields in MongoDB aggregation with count value in array?
- Can we search an array of objects in MongoDB?

Advertisements