简体   繁体   English

在MongoDB db.collection.find()中返回格式化的值

[英]Return formatted value in MongoDB db.collection.find()

I have a MongoDB JavaScript function saved in db.system.js, and I want to use it to both query and produce output data. 我有一个MongoDB JavaScript函数保存在db.system.js中,我想用它来查询和产生输出数据。

I'm able to query the results of the function using the $where clause like so: 我可以使用$ where子句查询函数的结果,如下所示:

db.records.find(
    {$where: "formatEmail(this.email.toString(), 'format type') == 'xxx'"},
    {email:1})

but I'm unable to use this to return a formatted value for the projected "email" field, like so: 但我无法使用它来返回预计的“电子邮件”字段的格式化值,如下所示:

db.records.find({}, {"formatEmail(this.email.toString(), 'format type')": 1}) 

Is there any way to do this while preserving the ability to simply use a pre-built function? 在保留简单使用预建功能的功能的同时,有什么办法可以做到这一点?

UPDATE: 更新:

Thank you all for your prompt participation. 谢谢大家的及时参与。

Let me explain why I need to do this in MongoDB and it's not a matter of client logic at the wrong layer.. What I am really trying to do is use the function for a shard bucketing value. 让我解释一下为什么需要在MongoDB中执行此操作,而不是在错误的层上进行客户端逻辑处理。.我真正想做的是将函数用于分片存储值。 Email was just one example, but in reality, what I have is a hash function that returns a mod value. 电子邮件只是一个示例,但实际上,我拥有的是一个返回mod值的哈希函数。

I'm aware of Mongo having the ability to shard based on a hashed value, but from what I gather, it produces a highly random value that can burden the re-balancing of shards with unnecessary load. 我知道Mongo能够基于散列值进行分片,但是据我收集,它会产生一个高度随机的值,可能会给分片的重新平衡带来不必要的负担。 So I want to control it like so func(_id, mod), which would return a value from 0 to say 1000 (depending on the mod value). 所以我想像func(_id,mod)这样控制它,它将返回一个从0到1000的值(取决于mod值)。

Also, I guess I would also like to use the output of the function in some sort of grouping scenario, and I guess Map Reduce does come to mind.. I was just hoping to avoid writing overly complex M/R for something so simple.. also, I don't really know how to do Map Reduce .. lol. 另外,我想我也想在某种分组方案中使用该函数的输出,并且我想Map Reduce确实会浮现在脑海。我只是希望避免为如此简单的事情编写过于复杂的M / R。也,我真的不知道该怎么做Map Reduce ..哈哈。

So, I gather that from your answers, there is no way to return any formatted value back from mongo (without map/reduce), is that right? 因此,我从您的答案中得出结论,无法从mongo返回任何格式化的值(没有map / reduce),对吗?

I think you are mixing your "layers" of functionality here -- the database stores and retrieves data, thats all. 我认为您在这里混合了功能的“层”-数据库存储和检索数据,仅此而已。 What you need to do is: * get that data and store the cursor in a variable * loop through your cursor, and for every record you go through * format and output your record as you see fit. 您需要做的是:*获取数据并将光标存储在变量中*遍历光标,对于每条记录,您都要进行格式化*并视需要输出记录。 This is somewhat similar to what you have described in your question, but its not part of MongoDB and you have to provide the "formatEmail" function in your "application layer" 这有点类似于您在问题中所描述的内容,但它不是MongoDB的一部分,您必须在“应用程序层”中提供“ formatEmail”功能

Hope it helps 希望能帮助到你

As @alernerdev has already mentioned, this is generally not done at a database layer. 正如@alernerdev已经提到的那样,通常不会在数据库层上完成此操作。 However, sometimes storing a pre-formatted version in your database is the way to go. 但是,有时需要在数据库中存储预格式化的版本。 Here's some instances where you may wish to store extra data: 在某些情况下,您可能希望存储其他数据:

  • If you need to lookup data in a particular format. 如果需要以特定格式查找数据。 For example, I have a "username" and a "usernameLowercase" fields for my primary user collection. 例如,我的主要用户集合有一个“用户名”和“ usernameLowercase”字段。 The lowercase'd one is indexed, and is the one I use for username lookup. 小写的一个被索引,这是我用于用户名查找的那个。 The mixed-case one is used for displaying the username. 大小写混合的一个用于显示用户名。

  • If you need to report a large amount of data in a particular format. 如果您需要报告特定格式的大量数据。 100,000 email addresses all formatted in a particular way? 100,000个以特定方式格式化的电子邮件地址? Probably best to just store them in that format in the db. 最好将它们以这种格式存储在数据库中。

  • If your translation from one format to another is computationally expensive. 如果您从一种格式转换为另一种格式在计算上是昂贵的。 Doubly so if you're processing lots of records. 令人怀疑的是,如果您要处理大量记录。

In this case, if all you're doing is looking up or retrieving an email in a specific format, I'd recommend adding a field for it and then indexing it. 在这种情况下,如果您要做的只是查找或检索特定格式的电子邮件,建议您为其添加一个字段,然后为其编制索引。 That way you won't need to do actual document retrieval for the lookup or the display. 这样,您就无需为查找或显示进行实际的文档检索。 Super fast. 超级快。 Disk storage space for something the size of an email address is super cheap! 像电子邮件地址一样大小的磁盘存储空间非常便宜!

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

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