最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoDB select and concatenate fields - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Dec 17, 2015 at 23:03 BatScream 19.7k4 gold badges53 silver badges68 bronze badges asked Nov 20, 2014 at 8:53 SunubaSunuba 702 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Use the aggregation operations as below:

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

评论列表(0)

  1. 暂无评论