- 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
Perform MongoDB array concatenation to concatenate records
For array concatenation, use $concatArrays operator. Let us first create a collection with documents −
>db.arrayConcatenationDemo.insertOne({"TeacherName":["Chris","Robert"],"StudentName":["Mike","Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ce921c078f00858fb12e911") }
Following is the query to display all documents from a collection with the help of find() method −
> db.arrayConcatenationDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce921c078f00858fb12e911"), "TeacherName" : [ "Chris", "Robert" ], "StudentName" : [ "Mike", "Sam" ] }
Following is the query for array concatenation −
> db.arrayConcatenationDemo.aggregate([ { "$project": { "StudentAndTeacherName": { "$concatArrays": [ "$TeacherName", "$StudentName" ] } }} ]);
This will produce the following output −
{ "_id" : ObjectId("5ce921c078f00858fb12e911"), "StudentAndTeacherName" : [ "Chris", "Robert", "Mike", "Sam" ] }
- Related Articles
- MongoDB query for array concatenation?
- MongoDB query to concatenate values of array with other fields
- How to perform string aggregation/concatenation in Oracle?
- How to concatenate results in MongoDB?
- Perform $lookup to array of object id's in MongoDB?
- Concatenate fields in MongoDB?
- Find MongoDB records where array field is not empty?
- C++ program to get length of strings, perform concatenation and swap characters
- How can I concatenate an array of integer in MongoDB aggregation method?
- Concatenate with condition in MongoDB?
- Display and concatenate records ignoring NULL values in MySQL
- MongoDB Convert One record with an array to multiple records in a new collection?
- How do you perform an AND query on an array in MongoDB?
- How to concatenate byte array in java?
- How to use GROUP BY to concatenate strings in MySQL and how to set a separator for the concatenation?

Advertisements