MongoDB中的多语言属性 [英] Multi-language attributes in MongoDB

查看:356
本文介绍了MongoDB中的多语言属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MongoDB中设计一个架构范例,该范例将支持文档中变量属性的多语言值.

I'm trying to design a schema paradigm in MongoDB which would support multilingual values for variable attributes in documents.

例如,我有一个产品目录,其中每个产品都可能需要以各种语言存储其名称,标题或任何其他属性. 对于其他特定于语言环境的属性,例如价格/货币变化,也应该使用相同的范例

For example, I would have a product catalog where each product may require storing its name, title or any other attribute in various languages. This same paradigm should probably hold for other locale-specific properties, such as price/currency variations

我一直在考虑键值方法,其中键是语言代码,值是相应的值:

I've been considering a key-value approach where key is the language code and value is the corresponding value:

 { 
      sku: "1011",
      name: { "en": "cheese", "de": "Käse", "es": "queso", etc... },
      price: { "usd": 30.95, "eur": 20, "aud": 40, etc... }
 } 

问题是我认为这将使我无法在多语言字段上使用索引. 最终,我想要一个通用但直观,可索引的设计.

The problem is I believe this would deny me of using indices on multilingual fields. Eventually, I'd like a generic, yet intuitive, index-able design.

任何建议将不胜感激,

推荐答案

有关架构设计的批发建议可能是一个广泛的话题,需要在此处进行讨论.但是,我建议您考虑将要显示的元素放入子文档的 Array 中,而不是将每个项目的字段都包含在单个子文档中.

Wholesale recommendations over your schema design may be a bit broad a topic for discussion here. I can however suggest that you consider putting the elements you are showing into an Array of sub-documents, rather than the singular sub-document with fields for each item.

{ 
    sku: "1011",
    name: [{ "en": "cheese" }, {"de": "Käse"}, {"es": "queso"}, etc... ],
    price: [{ "usd": 30.95 }, { "eur": 20 }, { "aud": 40 }, etc... ]
} 

主要原因是要考虑对元素的访问路径,这将使查询变得更容易.我在此处进行了详细介绍,也许值得您阅读.

The main reason for this is consideration for access paths to your elements which should make things easier to query. This I went through in some detail here which may be worth your reading.

也有可能对此进行扩展,例如您的姓名字段:

It could also be a possibility to expand on this for something like your name field:

    name: [
        { "lang": "en", "value": "cheese" },
        { "lang": "de", "value: "Käse"  },
        { "lang": "es", "value": "queso" },
        etc...
    ]

全部取决于您的索引编制和访问要求.这完全取决于您的应用程序确切需要什么,而MongoDB的优点在于,它使您可以根据需要构造文档.

All would depend on your indexing and access requirements. It all really depends on what exactly your application needs, and the beauty of MongoDB is that it allows you to structure your documents to your needs.

P.S关于存储 Money 值的任何内容,我建议您进行一些阅读,然后也许从这里开始这篇文章:

P.S As to anything where you are storing Money values, I suggest you do some reading and start maybe with this post here:

MongoDB-值的十进制类型如何?

这篇关于MongoDB中的多语言属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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