mongodb ORM是否允许您对字段进行别名? [英] do any mongodb ORMs allow you to alias fields?

查看:74
本文介绍了mongodb ORM是否允许您对字段进行别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看了这个: http://blog.mongodb.org/post/38467892360/mongodb-schema-design-insights-and-tradeoffs-from

一个建议是:在将被多次复制的文档中,尝试使字段名尽可能小:

One suggestion that came out of the talk: in docs that will be replicated many times, try to make the field names as small as possible:

始终通过使用短字段名称来减小集合大小 习俗.这将帮助您节省一段时间的内存.

Reduce collection size by always using short field names as a convention. This will help you save memory over time.

在"publicationUrl"上选择"u".如果您要谈论的是数百万行,这是有道理的.但是,那里存在较大的可读性问题.显而易见,该值是一个url,但是它是哪种类型的url?

Choose "u" over "publicationUrl". Makes sense if you're talking about millions of rows. However, big readability problem there. It might be obvious that the value is a url, but what sort of url is it?

这在ORM中可能是可解决的.是否有任何与MongoDb交互的ORM允许您说数据库中的"u"将映射为代码中的"publicationUrl"?当代码中有a.u之类的内容时,可读性很差; article.u并不好.

This might be solvable in the ORM though. Do any ORMs that interface with MongoDb allow you to say that 'u' in the db would map to 'publicationUrl' in the code? When you have things like a.u in code, that's pretty poor readability; article.u isn't much better.

(有Ruby和node.js标记是因为这些是我在mongo中使用的语言.请随时添加标记.)

(Ruby and node.js tags are there because those are the languages that I work with mongo in. Feel free to add tags.)

推荐答案

使用Ruby ORM可以轻松实现文档的示例:

This is easy to with the Ruby ORM Mongoid. Here is an example straight from the docs:

class Band
  include Mongoid::Document
  field :n, as: :name, type: String
end

band = Band.new(name: "Placebo")
band.attributes #=> { "n" => "Placebo" }

criteria = Band.where(name: "Placebo")
criteria.selector #=> { "n" => "Placebo" }

我已经在很多项目(尽管都是小型项目)上使用了Mongoid,并且非常喜欢与它一起工作.这些文档确实很棒,文档中也有关于性能的一节.

I have used Mongoid on quite a few projects (albeit, all small ones) and really enjoy working with it. The docs are really great, and there is a section in the docs about performance as well.

这篇关于mongodb ORM是否允许您对字段进行别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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