Multi-language attributes in MongoDB

前端 未结 1 1314
深忆病人
深忆病人 2021-02-09 08:36

I\'m trying to design a schema paradigm in MongoDB which would support multilingual values for variable attributes in documents.

For example, I would have a product cata

相关标签:
1条回答
  • 2021-02-09 08:57

    Wholesale recommendations over your schema design may be a bit broad a topic for discussion here. I can however suggest that you consider putting the elements you are showing into an Array of sub-documents, rather than the singular sub-document with fields for each item.

    { 
        sku: "1011",
        name: [{ "en": "cheese" }, {"de": "Käse"}, {"es": "queso"}, etc... ],
        price: [{ "usd": 30.95 }, { "eur": 20 }, { "aud": 40 }, etc... ]
    } 
    

    The main reason for this is consideration for access paths to your elements which should make things easier to query. This I went through in some detail here which may be worth your reading.

    It could also be a possibility to expand on this for something like your name field:

        name: [
            { "lang": "en", "value": "cheese" },
            { "lang": "de", "value: "Käse"  },
            { "lang": "es", "value": "queso" },
            etc...
        ]
    

    All would depend on your indexing and access requirements. It all really depends on what exactly your application needs, and the beauty of MongoDB is that it allows you to structure your documents to your needs.

    P.S As to anything where you are storing Money values, I suggest you do some reading and start maybe with this post here:

    MongoDB - What about Decimal type of value?

    0 讨论(0)
提交回复
热议问题