Multi-language attributes in MongoDB

南笙酒味 提交于 2019-12-03 07:10:17
Neil Lunn

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...
    ]

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 As to anything where you are storing Money values, I suggest you do some reading and start maybe with this post here:

MongoDB - What about Decimal type of value?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!