

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Concatenate with condition in MongoDB?
To concatenate with condition in MongoDB, use $cond and in that, work with $concat. Let us create a collection with documents −
> db.demo745.insertOne({Value1:"100",Value2:"100"}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6419a930c785c834e554") } > db.demo745.insertOne({Value1:"40",Value2:"50"}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6421a930c785c834e555") } > db.demo745.insertOne({Value1:"13",Value2:"45"}); { "acknowledged" : true, "insertedId" : ObjectId("5eae6429a930c785c834e556") }
Display all documents from a collection with the help of find() method −
> db.demo745.find();
This will produce the following output −
{ "_id" : ObjectId("5eae6419a930c785c834e554"), "Value1" : "100", "Value2" : "100" } { "_id" : ObjectId("5eae6421a930c785c834e555"), "Value1" : "40", "Value2" : "50" } { "_id" : ObjectId("5eae6429a930c785c834e556"), "Value1" : "13", "Value2" : "45" }
Followings is the query to concatenate with condition −
> db.demo745.aggregate( ... [ ... { "$redact": { ... "$cond": [ ... { "$eq": [ ... { "$concat": [ "$Value1","$Value2" ] }, ... "1345" ... ]}, ... "$$KEEP", ... "$$PRUNE" ... ] ... }} ... ] ... )
This will produce the following output −
{ "_id" : ObjectId("5eae6429a930c785c834e556"), "Value1" : "13", "Value2" : "45" }
- Related Questions & Answers
- Concatenate two tables in MySQL with a condition?
- How to select rows with condition via concatenate in MySQL?
- Concatenate fields in MongoDB?
- MongoDB query with an 'or' condition?
- Fetch multiple documents in MongoDB query with OR condition?
- How to concatenate results in MongoDB?
- Set $gt condition in MongoDB $and
- Set condition in MongoDB nested document?
- MongoDB query to concatenate values of array with other fields
- MongoDB query to insert an array element with a condition?
- Perform MongoDB array concatenation to concatenate records
- Apply a condition inside subset in MongoDB Aggregation?
- MongoDB query condition to compare two fields?
- MongoDB query condition on comparing 2 fields?
- Find MongoDB records based on a condition?
Advertisements