
- 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
Concatenate strings from two fields into a third field in MongoDB?
To concatenate strings from two fields into a third field, you can use the following syntax
db.yourCollectionName.aggregate( [ { $project: { "yourNewFieldName": { $concat: [ "$yourFieldName1", " yourDellimiterValue ", "$yourFieldName2" ] } } } ] );
Let us first create a collection with documents
>db.concatenateStringsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb7362d66697741252444") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb7402d66697741252445") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"Carol","StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb74c2d66697741252446") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"David","StudentLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb7752d66697741252447") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"James","StudentLastName":"Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb7862d66697741252448") }
Following is the query to display all documents from a collection with the help of find() method
> db.concatenateStringsDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9bb7362d66697741252444"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" : ObjectId("5c9bb7402d66697741252445"), "StudentFirstName" : "John", "StudentLastName" : "Smith" } { "_id" : ObjectId("5c9bb74c2d66697741252446"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor" } { "_id" : ObjectId("5c9bb7752d66697741252447"), "StudentFirstName" : "David", "StudentLastName" : "Miller" } { "_id" : ObjectId("5c9bb7862d66697741252448"), "StudentFirstName" : "James", "StudentLastName" : "Williams" }
Following is the query to concatenate strings from two fields into a third field. We are concatenating the fields “StudentFirstName” and “StudentLastName” here into a third field
> db.concatenateStringsDemo.aggregate( ... [ ... { $project: { "StudentFullName": { $concat: [ "$StudentFirstName", " / ", "$StudentLastName" ] } } } ... ] ... );
This will produce the following output
{ "_id" : ObjectId("5c9bb7362d66697741252444"), "StudentFullName" : "John / Doe" } { "_id" : ObjectId("5c9bb7402d66697741252445"), "StudentFullName" : "John / Smith" } { "_id" : ObjectId("5c9bb74c2d66697741252446"), "StudentFullName" : "Carol / Taylor" } { "_id" : ObjectId("5c9bb7752d66697741252447"), "StudentFullName" : "David / Miller" } { "_id" : ObjectId("5c9bb7862d66697741252448"), "StudentFullName" : "James / Williams" }
- Related Articles
- Concatenate fields in MongoDB?
- How to concatenate two strings in C#?
- How to concatenate two strings in Python?
- How to concatenate two strings in Golang?
- C++ Program to Concatenate Two Strings
- MongoDB query to concatenate values of array with other fields
- Merge two array fields in MongoDB?
- How to concatenate two strings using Java?
- Build complete path in Linux by concatenate two strings?
- Combine two MySQL fields and update a third one with result?
- Retrieving a Subset of Fields from MongoDB
- Removing empty fields from MongoDB
- How can we concatenate two strings using jQuery?
- MongoDB order by two fields sum?
- Concatenate strings in Arduino

Advertisements