Search code examples
mongodbname-value

Any tradeoffs for putting a set of name value pairs in a single field rather than each in their own field


We have a set of user data that is made up of name/value pairs represented in a single document. We are using mongodb to store that data and are thinking to just put the document of name/value pairs as a string within a single field in a collection rather than to create individual fields for each name/value pair within mongo and put each pair directly into its own field.

Does anyone have any input on why the former would be a bad idea. We are not expecting a lot of records per query and there is probably around 100 name/value pairs per record


Solution

  • Well, I don't see any benefit in storing it as a single string. Document size will be roughly similar, you wont be able to index the name fields of individual pairs and you can't query on the existence of specific name/value pairs. On the flipside I cannot think of a single benefit. The only real choice is whether to do this :

    {
        name1: value1,
        name2: value2,
        name3: value3
    }
    

    or

    [
        {
            name: name1,
            value: value1
        },
        {
            name: name2,
            value: value2
        },
        {
            name: name3,
            value: value3
        }
    ]