简体   繁体   English

MongoDB选择并连接字段

[英]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? 如何将此SQL转换为MongoDB查询?

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) . 在我的搜索过程中,由于concat ,我已经决定使用聚合框架,但我收到的只是concat(name, surname) projection ,而不是namesurnameconcat(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. 但它没有给我正确的json数组。

Any suggestions? 有什么建议么?

Use the aggregation operations as below: 使用如下聚合操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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