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?
-
learnmongodbthehardway.com/schema/multilanguageRobert Harvey– Robert Harvey2019年08月15日 15:41:41 +00:00Commented Aug 15, 2019 at 15:41
2 Answers 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.
-
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 titleobject.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.Gabriel Smoljar– Gabriel Smoljar2013年05月31日 19:23:10 +00:00Commented 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.Fer– Fer2013年06月03日 09:40:05 +00:00Commented 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)Fer– Fer2013年06月03日 09:42:40 +00:00Commented Jun 3, 2013 at 9:42
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