问题
I'm writing a service with sprint boot. And for the persistence layer I'm using MongoDB. There's some i18n cases in my service but I'm not going to use Mongoid. Should I design the tables like other db? Or is there any best practice of doing i18n with MongoDB? Thank you!
回答1:
You can put actual values in an array inside of your document. Something like that:
{
"_id" : "some id",
"defaultValue" : {
"_id" : "ENG",
"v" : "English Value"
},
"values" : [
{
"_id" : "ENG",
"v" : "English Value"
},
{
"_id" : "RUS",
"v" : "Russian Value"
}
]
}
When getting the value you can query with the document id plus values id to get desired language value. If desired language is not exist you can get default value.
db.yourDocument.find({"_id": "document id", "values._id": "ENG"}, {"values.$": 1})
If the query return no element check for default value:
db.yourDocument.find({"_id": "document id"}, {"defaultValue": 1})
来源:https://stackoverflow.com/questions/44110127/how-to-do-i18n-with-mongodb