简体   繁体   中英

MongoDB select and concatenate fields

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?

Use the aggregation operations as below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM