8

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?

asked May 18, 2013 at 7:45
1

2 Answers 2

2

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.

gnat
20.5k29 gold badges117 silver badges308 bronze badges
answered May 31, 2013 at 13:32
3
  • Why do you have a language object inside the title property? Why not use the language code as indexer? {'id': '123', 'title': {'en': 'english', 'sv': 'swedish' } } and then to access the title object.title['en'], alternatively add a getTitle() method that get's the title according to the user's locale. My main concern is whether I should keep meta and content in separate documents or not. What is more efficient? A single large document with all versions of an article, or one document for metadata and and separate documents with content for every language. Commented May 31, 2013 at 19:23
  • Hello, It really depends in how are you planning to access your data and from where comes your data, in my case, I have "titles" that are translated on English and another "titles" that are translated on German and not neccessary have a version on English. And I want to index (Using Mongo EnsureIndex) on all the texts fields of my collaction, It is why I need to have the texts in an array, If I had the texts as you proposed ('title':{'en','english,'sv':'swedish'}) then I would be unable to index all the texts without knowing which languages to index. Commented Jun 3, 2013 at 9:40
  • I usually have metadata in the same document as this data is small, if there is a lot of metadata, then I could think in separates documents, or only asking parts of the document when querying the data. I thing from the mongo point of view, to optimize performance, all depends on your indexes and the grannularity of the quary (I mean you can ask only parts of a document) Commented Jun 3, 2013 at 9:42
1

I use following pattern for key and values that should be indexed in key:

{
"id":"ObjectId",
"key":"error1"
"values":[{
 "lang":"en", 
 "value":"Error Message 1"
 },
 {
 "lang":"fa", 
 "value":"متن خطای شماره 1"
 }] 
}

and Use This Code in C#

 object = coleccion.find({"key": "error1"});

view this link Model One-to-Many Relationships with Embedded Documents!

I Reshare This Post

answered Jul 14, 2019 at 5:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.