Multilingual data modeling on MongoDB

Abdel Raoof Olakara picture Abdel Raoof Olakara · May 22, 2014 · Viewed 13.6k times · Source

I am trying to model my objects on MonogoDB and not sure how to proceed. I am building a Product catalog that will be:

  • No frequent changes to product catalog. A bulk operation may be done weekly / fortnight.
  • Product information is in multiple languages ( English, Spanish , French ) new language may be added anytime.

Here is what I am trying to do: I need to model my product catalog to capture the multilingual functionality. Assume I have:

product : { 
 _id:xxx,
 sku:"23456",
 name:"Name",
 description: "Product details", 
 tags:["x1","x2"]}... 
}

Surely, name,description, tags and possible images will change according to language. So, how do I model it?

  1. I can have a seperate collection for each language eg: enProducts,esProducts etc
  2. Have JSON representation in the product itself with the individual languages like:

    product :{
       id: xxx,
       en: {
             name: "Name",
             description: "product details.."
           },
       es: {
             name: "Name",
             description: "product details.."
           },
       ...   
    }
    


Or is there any other solution? Need help of MongoDB modeling experts here :)

Answer

Adam Gerthel picture Adam Gerthel · Feb 22, 2018

Another option would be to just keep the values different per language. Would probably make maintaining the schema much easier as well:

product : { 
 _id:xxx,
 sku: {
   und: "23456"
 },
 name: {
   en: "Fork",
   de: "Gabel"
 },
 description: {
   en: "A metal thingy with four spikes",
   de: "Eine Dinge aus metal der vier spitze hat"
 }  
}

und would be short for "undefined", i.e. the same for all languages, and could be used as a fallback - or you always use "en" as fallback if you'd prefer that.

The above example is roughly how Drupal CMS manages languages (albeit translated from SQL to Mongo).