在Find上转换MongoDB数据 [英] Transform MongoDB Data on Find

查看:116
本文介绍了在Find上转换MongoDB数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从MongoDB中的Find查询中转换返回的数据?



举例来说,我有一个第一个上一个字段来存储用户的名字和姓氏。在某些查询中,我希望仅返回名字和最后的首字母(例如'Joe Smith'返回为'Joe S')。在MySQL中, SUBSTRING()函数可用于 SELECT 语句中的字段。



Mongo中的数据转换或字符串函数与SQL中的一样吗?如果可以,请提供一个使用示例。如果没有,是否有一种方法将数据转化为循环通过返回的对象?解析方案

有可能做几乎任何服务器端与MongoDB。你通常会听到不的原因是你在普通情况下牺牲了太多速度让它变得有意义。 PyMongo的主要力量之一,10gen的Mike Dirolf,在这里使用服务器端javascript与pymongo写了一篇很好的博客文章: http://dirolf.com/2010/04/05/stored-javascript-in-mongodb-and-pymongo.html 。他的例子是存储一个javascript函数来返回两个字段的总和。但是,您可以轻松修改以返回用户名字段的第一个字母。要点如下:

  db.system_js.first_letter =function(x){return x.charAt(0) ;}

首先要明白,mongodb非常适合检索数据,不是很擅长处理它。这个建议(例如Oreilly的Kristina Chodorow的mongodb开发人员提供的50个技巧和窍门)就是完成Andrew特意提到的上述操作:创建第一个字母列并返回。任何处理都可以在应用程序中更高效地完成。但是,如果您在从'视图'返回完整名称[0]之前甚至查询完整名称会带来太多的安全风险,那么您不需要尽一切可能的最快方式。由于所有公众对速度的担忧,我暂时避免在MongoDB中使用map-reduce。然后我跑了我的第一张地图缩小版,把我的拇指旋转了0.1秒,因为它处理了80,000个10k文件。我在事情的计划中意识到,那很微小。但它说明,仅仅因为一个庞大的网站在某些服务器端处理上的性能受到影响是不利的,并不意味着它对您而言就很重要。在我的例子中,我想我需要花一点时间才能迁移到Hadoop,而不是每隔一段时间就吃那个.1秒。祝你网站好运

Is it possible to transform the returned data from a Find query in MongoDB?

As an example, I have a first and last field to store a user's first and last name. In certain queries, I wish to return the first name and last initial only (e.g. 'Joe Smith' returned as 'Joe S'). In MySQL a SUBSTRING() function could be used on the field in the SELECT statement.

Are there data transformations or string functions in Mongo like there are in SQL? If so can you please provide an example of usage. If not, is there a proposed method of transforming the data aside from looping through the returned object?

解决方案

It is possible to do just about anything server-side with mongodb. The reason you will usually hear "no" is you sacrifice too much speed for it to make sense under ordinary circumstances. One of the main forces behind PyMongo, Mike Dirolf with 10gen, has a good blog post on using server-side javascript with pymongo here: http://dirolf.com/2010/04/05/stored-javascript-in-mongodb-and-pymongo.html. His example is for storing a javascript function to return the sum of two fields. But you could easily modify to return the first letter of your user name field. The gist would be something like:

db.system_js.first_letter = "function (x) { return x.charAt(0); }"

Understand first, though, that mongodb is made to be really good at retrieving your data, not really good at processing it. The recommendation (see for example 50 tips and tricks for mongodb developers from Kristina Chodorow by Oreilly) is to do what Andrew tersely alluded to doing above: make a first letter column and return that instead. Any processing can be more efficiently done in the application.

But if you feel that even querying for the fullname before returning fullname[0] from your 'view' is too much of a security risk, you don't need to do everything the fastest possible way. I'd avoided map-reduce in mongodb for awhile because of all the public concerns about speed. Then I ran my first map reduce and twiddled my thumbs for .1 seconds as it processed 80,000 10k documents. I realize in the scheme of things, that's tiny. But it illustrates that just because it's bad for a massive website to take a performance hit on some server side processing, doesn't mean it would matter to you. In my case, I imagine it would take me slightly longer to migrate to Hadoop than to just eat that .1 seconds every now and then. Good luck with your site

这篇关于在Find上转换MongoDB数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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