How to do i18n with MongoDB?

六眼飞鱼酱① 提交于 2021-01-28 07:26:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!