MongoDB select and concatenate fields

javascript, mongodb

Solution

Use the aggregation operations as below:

db.collection.aggregate([
{$project:{"name_surname":{$concat:["$name","-","$surname"]},"name":1,"surname":1}}
])

Problem

I have a very basic question: ``` SELECT name, surname CONCAT(name, surname) AS name_surname from users; ``` How can I convert this `SQL` to `MongoDB` query? During my search, I have decided that it is possible with aggregate framework due to `concat`, but what I received is only `projection` of `concat(name, surname)` not `name`, `surname` and `concat(name, surname)`. Final thing I got is this query: ``` db.inroamers.find().forEach( function(o) { print(o.LAC + '-' + o.CELL + ' ' + o.CHARGE + ' ' + o.WEEK); }) ``` but it does not give me proper `json` array. Any suggestions?

Original source