jsonb vs jsonb[] for multiple addresses for a customer

后端 未结 1 469
走了就别回头了
走了就别回头了 2020-12-02 01:24

It\'s a good idea to save multiple addresses in a jsonb field in PostgreSQL. I\'m new in nosql and I\'d like to test PostgreSQL to do that. I don\'t want to have another tab

相关标签:
1条回答
  • 2020-12-02 01:41

    Use a jsonb (not jsonb[]!) column with the structure like this:

    select
    '[{
            "adresse_line-1": "a11",
            "adresse_line-2": "a12",
            "postalcode": "code1"
        },
        {
            "adresse_line-1": "a21",
            "adresse_line-2": "a22",
            "postalcode": "code2"
        }
    ]'::jsonb;
    

    Though, a regular table related to the main one is a better option.

    Why not jsonb[]? Take a look at JSON definition:

    JSON is built on two structures:

    • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
    • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

    In a jsonb column you can therefore store an array of objects. Attempts to use the array of jsonb are probably due to misunderstanding of this type of data. I have never seen a reasonable need for such a solution.

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