Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-language attributes in 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.

Any suggestion would be appreciated, thanks.

like image 420
ChenR Avatar asked Feb 09 '14 00:02

ChenR


People also ask

What are best practices for multi language database design?

For an application and its database to be truly multi-lingual, all texts should have a translation in each supported language – not just the text data in a particular table. This is achieved with a translation subschema where all data with textual content that can reach the user's eyes is stored.

What is GTE and LT in MongoDB?

Comparison Operators Matches if values are greater than the given value. $lt. Matches if values are less than the given value. $gte. Matches if values are greater or equal to the given value.

What is $Not in MongoDB?

$not performs a logical NOT operation on the specified <operator-expression> and selects the documents that do not match the <operator-expression> . This includes documents that do not contain the field .


1 Answers

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?

like image 92
Neil Lunn Avatar answered Sep 25 '22 23:09

Neil Lunn