mongodb 相当于 sql select 'longfield' as 'a' [英] mongodb equivalent of sql select 'longfield' as 'a'

查看:66
本文介绍了mongodb 相当于 sql select 'longfield' as 'a'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通读MongoDB文档判断,没有类似sql的语句

Judging by reading through MongoDB documentation, there is no equivalent of sql-like statement

SELECT 'field1' as 'a', 'field2' as 'b' ....

但是因为我需要这样的功能,我希望我错过了一些东西.

but because I am in need of such feature, I hope that I missed something.

处理这个问题的常见做法是什么?

What is the common practice for dealing with this problem?

推荐答案

最常见的方法是在您的应用程序中创建一个映射层来接收一组字段,然后将它们映射到您的应用程序需要的内容.

The most common approach is to make a mapping layer in your application to take in a set of fields and then map them to what your application needs.

您说得对,MongoDB 中根本没有内置 AS 投影.

You are correct that there is not AS projection built into MongoDB at all.

我相信您可以通过聚合来实现这一点:

I believe you could accomplish this with the aggregation however:

  • 我不明白为什么
  • 与仅在应用中进行相比,它会更重、更慢.

所以是的,我会坚持在应用程序中编写映射类.

So yea, I would stick to writing a mapping class in the app.

这篇关于mongodb 相当于 sql select 'longfield' as 'a'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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