A good pattern for multi language in MongoDb

mongodbmultilingualnosql

Say I want to build a website that support multiple language and use MongoDB datastore. I wonder what is a good approach to have multiple versions of the body and title of an article, one for each language.

Should I keep all versions in the same article document? Like a collection of key-value pairs of language code and text in the body field of the article. This way I'd get no more than 1 request for viewing articles, but I'd also get more data than I might need so the response is larger.

Is there a better solution? How and why?

Best Answer

I use the following pattern for text that should be indexed in all the languages:

{
"id":"sdsd"
"title":{"languages":{"en":0,"fr":1},"texts":["this is the title in inglish","Celui ci c'est le titre en francais"]}
}

object = coleccion.find("id='xxxxx'");

// now if want the text in English

print(object.title.texts[object.title.languages["en"]])

// the use of objecttitle.languages index array it to improve performance in client accessing a determined text

// that allows us to add indexes to your translated texts on mongo, as

ensureIndex({title.texts})

We can also wrap the code to obtain text in an specific language in a Class.

Related Topic