1

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

Vinae
  • 13
  • 2
  • What do you think is advantage of storing everything in one big string? I can't think of any. – TTT Aug 11 '11 at 12:27
  • I wasn't convinced there was either. There is an existing structure out in the model that is a string (well, document) and we are re-designing so trying to establish if we should bite the bullet and make the change in the way we handle data as well – Vinae Aug 11 '11 at 22:01

2 Answers2

2

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
    }
]
Remon van Vliet
  • 18,365
  • 3
  • 52
  • 57
  • 1
    You're very welcome. FYI, the top approachis more appropriate when you know which fields are going to be in the document (at least the ones you need an index on) and will result in smaller documents. The latter approach is appropriate for situations where you do want an index on the "field" without knowing in advance what the fields will be. – Remon van Vliet Aug 12 '11 at 08:33
0

What is the recommended way to index the latter case, if you'll need to search where "name=xxx" AND "value="xxx". Should the entire sub document be indexed, or each field (e.g. "name" and "value"?

CoreyS
  • 1
  • It's best to open a new question with this answer for future reference. Helps other people. But to answer the question, you can use either but the queries would look different in each case. Link me to a newly opened question and I will elaborate. – Remon van Vliet Nov 22 '11 at 09:08