MongoDB选择并连接字段 [英] MongoDB select and concatenate fields

查看:94
本文介绍了MongoDB选择并连接字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的问题:

SELECT name, surname CONCAT(name, surname) AS name_surname from users;

如何将该SQL转换为MongoDB查询?

在搜索过程中,由于concat,我已经决定使用聚合框架是可能的,但是我收到的只是concat(name, surname)中的projection,而不是namesurnameconcat(name, surname). /p>

我最后得到的是这个查询:

db.inroamers.find().forEach(
                 function(o) { 
                 print(o.LAC + '-' + o.CELL + ' ' + o.CHARGE + ' ' + o.WEEK);
               })

但是它不能给我正确的json数组.

有什么建议吗?

解决方案

使用以下聚合操作:

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

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}}
])

这篇关于MongoDB选择并连接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆